I use rxjava in my Android application to asynchronously process network requests. Now I would like to retry the failed network request only after a while.
Is it possible to use the retry () function in Observable, but only repeat after some delay?
Is there a way to tell Observable that it is currently retrying (as opposed to trying the first time)?
I looked at debounce () / throttleWithTimeout (), but they seem to be doing something else.
Edit:
I think I found one way to do this, but I would be interested to confirm that this is the right way to do this, or for other, better ways.
What I am doing is: In the call () method of my Observable.OnSubscribe, before I call the Subscribers onError () method, I just allow Thread sleep for the required amount of time. So, to repeat every 1000 milliseconds, I am doing something like this:
@Override public void call(Subscriber<? super List<ProductNode>> subscriber) { try { Log.d(TAG, "trying to load all products with pid: " + pid); subscriber.onNext(productClient.getProductNodesForParentId(pid)); subscriber.onCompleted(); } catch (Exception e) { try { Thread.sleep(1000); } catch (InterruptedException e1) { e.printStackTrace(); } subscriber.onError(e); } }
Since this method works on an I / O stream, it does not block the user interface. The only problem I see is that even the first error is reported with a delay, so the delay exists even if there is no retry (). I would like it to be better if the delay was not applied after the error, but instead before retrying (but not before the first attempt, obviously).
rx-java
david.mihola Feb 27 '14 at 11:00 2014-02-27 11:00
source share