Is it a bad idea to subscribe another observable inside doOnNext?

589 views
Skip to first unread message

Raymond Chan

unread,
Aug 25, 2016, 6:07:05 AM8/25/16
to RxJava
I'm a bit new to RxJava and currently I'm migrating some old code to reactive approach.
Here's the situation, I would like to display a list of posts from local or remote

Observable<List<Post>> local = createLocalObservable();
Observable<List<Post>> remote = createRemoteObservable();
Observable<List<Post>> combined = Observable.concat(local, remote)
                               
.doOnNext(o -> {
                                   
//... some logging etc...
                               
});


In the old logic, I have an extra API call inside the remote API call returned and run in a separate thread.

So my question is, how can I make the extra API call with single Observable?

What I came up so far:

createRemoteObservable()
.doOnNext(posts -> {
    extraApiCallObservable
(posts)
   
.subscribeOn(Schedulers.io())
   
.subscribe();
});



However, that doesn't look nice and moreover, I may need to update the UI according to the extra API call's result.
How can I achieve parallelism for this case?

Łukasz Marczak

unread,
Aug 30, 2016, 8:30:10 AM8/30/16
to RxJava
Maybe zip operator will help you with parallel execution:
Observable.zip(o1(),o2(), new Func2<...>)
.map(..) and so on.

George Campbell

unread,
Aug 31, 2016, 1:46:50 AM8/31/16
to RxJava
What about flatMap.

        createRemoteObservable().flatMap(posts -> {

            return extraApiCallObservable(posts).subscribeOn(Schedulers.io()).toCompletable().andThen(Observable.just(posts));

        });

Reply all
Reply to author
Forward
0 new messages