In RxJava, how to retry / resume an error, instead of completing the observable

What I want to achieve:

  • Monitor settings for a specific change
  • when a change is detected, start a new network call using the new value
  • conversion result
  • UI display result

I know when the change will happen, now I assume that I need to call onNext on the topic. Then this should initiate the Rx chain, and in the end I can update the interface.

mViewPeriodSubject = PublishSubject.create(); mAdapterObservable = mViewPeriodSubject .flatMap(period -> MyRetrofitAPI.getService().fetchData(period)) // this might fail .flatMap(Observable::from) .map(MyItem::modifyItem) .toList() .map(obj -> new MyAdapter(obj)); mViewPeriodSubject.onNext("week"); // this one starts the chain mViewPeriodSubject.onNext("year"); // this one does not 

But in the event of a network call failure, the observed errors and the onNext () call do not result in another network call.

So my question is: how do I do this? How can I keep the Observable intact so that I can just throw another value into it? I can imagine, for example, a simple redo button that just wants to ignore the fact that an error has occurred.

+4
source share
1 answer

You do not need to deal with your mViewPeriodSubject error, but instead deal with errors in your updated Observable. This modified Observable will not resume, but at least it will not affect your β€œprimary” observation.

 mAdapterObservable = mViewPeriodSubject .flatMap(period -> MyRetrofitAPI.getService().fetchData(period).onErrorResumeNext(e -> Observable.empty()) // this might fail .flatMap(Observable::from) .map(MyItem::modifyItem) .toList() .map(obj -> new MyAdapter(obj)); 
+6
source

All Articles