Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modify Source Observable on retry - RxJava

Tags:

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?

like image 584
Bharath Avatar asked Oct 24 '16 04:10

Bharath


2 Answers

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

like image 87
drhr Avatar answered Sep 25 '22 16:09

drhr


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);
        });
like image 40
ehehhh Avatar answered Sep 22 '22 16:09

ehehhh