Last active
February 1, 2016 02:00
-
-
Save danylovolokh/3a44a03b3865b9088397 to your computer and use it in GitHub Desktop.
Server polling and retry operations when failed. With Retrofit and RxJava.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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