仅重试失败的可观察RxJava 2

时间:2016-12-01 19:56:52

标签: android rx-android rx-java2

我试图运行一个长时间运行的任务,该任务可能会失败,我尝试重试列表中的某些对象,但它会重新订阅整个observable列表。我可以做嵌套订阅,但似乎错了。有没有比嵌套订阅更好的解决方案?

这是我的实施:

public Observable<ReportItemModel> deferReports() {
        return  Observable.defer(new Callable<ObservableSource<? extends ReportItemModel>>() {
            @Override
            public ObservableSource<? extends ReportItemModel> call() throws Exception {
                return Observable.fromIterable(getReports())
                        .map(new Function<Report, ReportItemModel>() {
                            @Override
                            public ReportItemModel apply(Report report) throws Exception {
                                return report.getReport();
                            }
                        });
            }
        });
}

reportFactory.deferReports()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .retry()
            .subscribe(new Observer<ReportItemModel>() {
                @Override
                public void onSubscribe(Disposable d) {
                }

                @Override
                public void onNext(ReportItemModel value) {
                    Log.d(TAG,value.toString());
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onComplete() {
                }
            });

0 个答案:

没有答案