I am upgrading to rxjava2, we have the code to poll the data from server, the code handles to retry with delays when there is a network issue. However, somehow when i was trying to migrate to rxjava2, the code stops working. Here is the code for Rxjava1, and its working perfectly , basically followed this http://blog.danlew.net/2015/03/02/dont-break-the-chain/ and this https://medium.com/@v.danylo/server-polling-and-retrying-failed-operations-with-retrofit-and-rxjava-8bcc7e641a5a
.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
@Override
public Observable<?> call(final Observable<? extends Throwable> observable) {
// wrap a flatmap here so that i can check the exception type
return observable.flatMap(new Func1<Throwable, Observable<?>>() {
@Override
public Observable<?> call(Throwable throwable) {
mThrowable = throwable;
if (throwable instanceof IOException) {
return observable.compose(timerWithRetries());
} else {
// for other errors, call onError and exit
return Observable.error(throwable);
}
}
});
}
})
private <T> Observable.Transformer<T, Long> timerWithRetries() {
return new Observable.Transformer<T, Long>() {
@Override
public Observable<Long> call(Observable<T> observable) {
return observable
.zipWith(Observable.range(COUNTER_START, MAX_RETRIES + 1),
new Func2<T, Integer, Integer>() {
@Override
public Integer call(T t, Integer repeatAttempt) {
return repeatAttempt;
}
})
.flatMap(new Func1<Integer, Observable<Long>>() {
@Override
public Observable<Long> call(Integer repeatAttempt) {
if (repeatAttempt == MAX_RETRIES + 1) {
if (mThrowable instanceof IOException) {
// Custom Exception
throw new Exception();
}
}
// increase the waiting time
return Observable.timer(repeatAttempt * mDelaySeconds, TimeUnit.SECONDS);
}
});
}
};
}
I want to wrap the error with flatmap so that i can check the exception type and when it reaches to the maximum retries, i can pass my custom exception to onError.
However, when just use Rxjava2, the timerWithRetries() method stops working, this method is called but the .zipWith() and its flatmap are not executed.
But its working without the flatmap when wrapping the error, which is very weird. Something like
.retryWhen(error -> error.compose(timerWithRetries()))
Much appreciated with any suggestions!