1
votes

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.

1
Rxjava gives you various Operators that can help you, you can try with those operators for eg. retry, retrywhen, debounce etcRaut Darpan
if the problem only in token refreshing, than you can try native Authenticator from okhttp client i described it in other question: stackoverflow.com/questions/40762911/…borichellow

1 Answers

2
votes

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();