Last active
September 7, 2016 21:04
-
-
Save ragdroid/6554c545b50b3764c56b92edadaf8789 to your computer and use it in GitHub Desktop.
Example of RxJava repeat and retry operators - DroidconIN 2016
This file contains hidden or 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
// Example of repeat and retry | |
private Observable<Boggart> riddikulus() { | |
return getBoggartObservable() | |
.flatMap(new Func1<Boggart, Observable<Boggart>>() { | |
@Override | |
public Observable<Boggart> call(Boggart boggart) { | |
boggart.init(); | |
boggart.riddikulus(); | |
if (boggart.isFunny()) { | |
return Observable.just(boggart); | |
} | |
return Observable.error(new BoggartException()); | |
} | |
}) | |
.retry() | |
.repeat(10) // repeat 10 times | |
// take until Boggart is not Hilarious | |
.takeUntil(new Func1<Boggart, Boolean>() { | |
@Override | |
public Boolean call(Boggart boggart) { | |
return boggart.isHilarious(); | |
} | |
}) | |
// filter the input to only get the final Hilarious event | |
.filter(new Func1<Boggart, Boolean>() { | |
@Override | |
public Boolean call(Boggart boggart) { | |
return boggart.isHilarious(); | |
} | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment