问题
How do i update a source observable on retry?
List<String> ids = new ArrayList<>(); // A,B,C
Observable.from(ids)
.retryWhen(errors -> {
return errors
.zipWith(Observable.range(0, 1), (n, i) -> i)
.flatMap(retryCount -> Observable.timer((long) Math.pow(2, retryCount), TimeUnit.MINUTES));
})
.subscribe(....);
now rather than passing //A,B,C as ids if i want to pass some other values. How do i do it? or is this even the right approach?
回答1:
Use defer
. This would allow ids
to be re-computed:
Observable.defer(() -> {
List<String> ids = // compute this somehow
return Observable.from(ids);
}).retryWhen(...
Documentation on the defer operator
回答2:
onErrorResumeNext
could be used. You probably need some additional logic to match your use case. Documentation for error handling operators here.
List<String> ids = new ArrayList<>(); // A,B,C
List<String> ids2 = new ArrayList<>(); // D,E,F
Observable.from(ids)
.onErrorResumeNext(throwable -> {
return Observable.from(ids2);
});
来源:https://stackoverflow.com/questions/40210655/modify-source-observable-on-retry-rxjava