Modify Source Observable on retry - RxJava

十年热恋 提交于 2021-02-07 14:32:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!