0
votes

I am new to Rxjava. I am following a video tutorial where there is a code example that calls an API and gets string result using flatmap. Below is the code:

twitchAPI.getTopGamesObservable()
                .flatMap(new Func1<Twitch, Observable<Top>>() {
            @Override
            public Observable<Top> call(Twitch twitch) {
                Observable<Top> rtnTop =  Observable.from(twitch.getTop());
                Log.d("LogRx", "Size for FROM:"  + "\n");

                return rtnTop;
            }
        })
                .flatMap(new Func1<Top, Observable<String>>() {
            @Override
            public Observable<String> call(Top top) {
                Log.d("LogRx", "Size for JUST:"  + "\n");
                return Observable.just(top.getGame().getName());
            }
        })
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {
            @Override
            public void onCompleted() {     }

            @Override
            public void onError(Throwable e) {    }

            @Override
            public void onNext(String s) {  Log.d("LogRx", s);      }

        });

But i am not really understanding the sequence of how this code works.

And the confusion is because of observable.from() & observable.just(). I know that observable.from() will give N emissions for a list and observable.just() will give 1 emissions(a list).

So, does it mean that the second flatMap is called N times because Observable.from() inside the first flatMap is emitting each item in the list each time. And for the second flatMap the onNext() of subscriber is called N times too(once for each call of 2nd flatmap)?

1

1 Answers

0
votes

So, does it mean that the second flatMap is called N times because Observable.from() inside the first flatMap is emitting each item in the list each time.

The second flat map (.flatMap(new Func1<Top, Observable<String>>() {) is called twitch.getTop().size() times.

And for the second flatMap the onNext() of subscriber is called N times too

This is true. You get one observable, each with one value, per Top. The number of values delivered to onNext is equal to the number of Tops.

Which makes the second flat map kinda bloated. You might as well map a Top to String.


Unrelated: You may want to find a more recent tutorial. RxJava1 died almost a year ago.

RxJava 1.x is now officially end-of-life (EOL). No further developments, bugfixes, enhancements, javadoc changes or maintenance will be provided by this project after version 1.3.8 and after [31 Mar 2018].

Users are encouraged to migrate to 2.x. In accordance, the wiki will be updated in the coming months to describe 2.x features and properties.

Source: https://github.com/ReactiveX/RxJava/releases/tag/v1.3.8