Skip to content

Instantly share code, notes, and snippets.

@danylovolokh
Last active February 1, 2016 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danylovolokh/3a44a03b3865b9088397 to your computer and use it in GitHub Desktop.
Save danylovolokh/3a44a03b3865b9088397 to your computer and use it in GitHub Desktop.
Server polling and retry operations when failed. With Retrofit and RxJava.
private static final int COUNTER_START = 1;
private static final int ATTEMPTS = 5;
private static final int ORIGINAL_DELAY_IN_SECONDS = 10;
// this is new methods chain in repeatWhen predicate call function
repeatWhen(new Func1<Observable<? extends Void>, Observable<?>>() {
@Override
public Observable<?> call (Observable < ?extends Void > observable){
Log.v(TAG, "repeatWhen, call");
return observable.zipWith(Observable.range(COUNTER_START, ATTEMPTS), new Func2<Void, Integer, Integer>() {
@Override
public Integer call(Void aVoid, Integer attempt) {
Log.v(TAG, "zipWith, call, attempt " + attempt);
return attempt;
}
}).flatMap(new Func1<Integer, Observable<?>>() {
@Override
public Observable<?> call(Integer repeatAttempt) {
Log.v(TAG, "flatMap, call, repeatAttempt " + repeatAttempt);
// increase the waiting time
return Observable.timer(repeatAttempt * ORIGINAL_DELAY_IN_SECONDS, TimeUnit.SECONDS);
}
});
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment