Slide 8
Slide 8 text
Retrying requests -
retryWhen()
RxJava has retryWhen()!
retryWhen() can be a little tricky to
grok. Let's walk through it:
● retryWhen() is called once
per subscription
● It's given an Observable of the
errors by the parent stream
(Observable)
● As long as the Observable
returned from retryWhen()
does not complete or error, the
parent will be resubscribed
// this code retries the request three times, with a 5s delay,
// then 10s, then 15s
getRequestObservable()
.retryWhen(attempt -> {
return attempt
.zipWith(Observable.range(1, 3), (n, i) -> i)
.flatMap(i ->
return Observable.timer(5 * i, TimeUnit.SECONDS);
})
})
.subscribe(viewModel -> {
// handle updated request state
});