Nicolas Jafelle
Nicolas Jafelle

Reputation: 2771

RxJava wait for one observable with subscriber wait for another observable with other subscriber

I have this question that I have been searching for but could not find a solution (or maybe I cannot make the solution based on other answers).

My problem is that I need to find a way to wait for an observable (with its own subscriber) and wait for another observable (with its own subscriber) to finish.

This is the scenario:

obs1 -> retryWith(tokenAuthenticator) -> subscriber1 -> Update UI

obs2 -> Needs to wait for obs1 to get new token -> subscriber2 -> Update UI

My main concerns is that I need two subscribers. For my point of view obs1 and obs2 run in parallel but needs to check if the obs1 finish with new session tokens. Maybe this is not the main purpose of RxJava.

Obs1 connect to an HTTP connection and get a Json with data to fill in the UI and the same for obs2 but with other json and other information to fill in the UI.

The only problem for me is that my session token expires every 5 minutes and when I try to get new ones obs2 already make the call with expired session tokens.

The simple solution is to execute obs2 in the onComplete() subscriber1 but I know it has to be a better solution for this, using RxJava.

Upvotes: 1

Views: 1362

Answers (1)

Dave Moten
Dave Moten

Reputation: 12097

Look at zip which will allow you to combine the results of two observables (running in parallel if you want). Here's an example:

obs1.subscribeOn(Schedulers.io())
    .zipWith(obs2.subscribeOn(Schedulers.io()), (x,y) -> new Both(x,y))
    .doOnNext(both -> updateUI(both))
    .subscribe();

Upvotes: 2

Related Questions