Well, for everyone who is still interested, I think I have a better way to achieve this with rx.
The main note is to use onErrorResumeNext, which will allow you to replace Observable in case of an error. so it should look something like this:
Observable<Object> apiCall = createApiCallObservable().cache(1);
Thus, if the first call failed, the future call will simply call it (only once).
but every other caller who tries to use the first observable will fail and make another request.
You have made a reference to the original observable, just update it.
so lazy getter:
Observable<Object> apiCall; private Observable<Object> getCachedApiCall() { if ( apiCall == null){ apiCall = createApiCallObservable().cache(1); } return apiCall; }
now, the recipient that will retry if the previous one failed:
private Observable<Object> getRetryableCachedApiCall() { return getCachedApiCall().onErrorResumeNext(new Func1<Throwable, Observable<? extends Object>>() { public Observable<? extends Object> call(Throwable throwable) { apiCall = null; return getCachedApiCall(); } }); }
Please note that it will only repeat once for each call.
So, now your code will look something like this:
---------------------------------------------
ndori
source share