Reputation: 1516
In an Android app scenario, I want to fetch some Observable<Data>
from network, and there are multiple Observer<Data>
subscribed to it to update corresponding views. In case of error -say a timeout- show a button to the user to try again.
How can I do the try again part? can I tell the observable to re-execute its logic again without re-subscribing to it?
Upvotes: 2
Views: 634
Reputation: 70017
Let's assume you have two buttons, "Retry" and "Cancel", initially hidden. Create two Observable
s retryButtonClicks
and cancelButtonClicks
. Then, apply the retryWhen
operator to the designated download flow and act upon the signals of these button clicks:
download.retryWhen(errors -> {
return errors
.observeOn(AndroidSchedulers.mainThread())
.flatMap(e -> {
// show the "Retry" and "Cancel" buttons around here
return Observable.amb(
retryButtonClicks.take(1).map(v -> "Retry"),
cancelButtonClicks.take(1).map(v -> "Cancel")
)
.doOnNext(v -> { /* hide the "Retry" and "Cancel" buttons */ });
})
.takeWhile(v -> "Retry".equals(v))
;
});
Upvotes: 2
Reputation: 11110
There is actually specific methods
retry()
Returns an Observable that mirrors the source Observable, resubscribing to it if it calls onError (infinite retry count).
and retry(long count)
Returns an Observable that mirrors the source Observable, resubscribing to it if it calls onError up to a specified number of retries.
Read more in an article and in the docs
Upvotes: 1