问题
I am trying to re-execute a defined observable that failed. Using Retrofit2 and RxJava2 together i want to retry a specific request with its subscription and behavior when clicking a button. is that possible?
service.excecuteLoginService(url,
tokenModel,
RetrofitManager.apiKey)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(....)
回答1:
An option is to create Publisher, which emission is controlled by your button.
final PublishSubject<Object> retrySubject = PublishSubject.create();
service.excecuteLoginService(url,
tokenModel,
RetrofitManager.apiKey)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnError(throwable -> showButton())
.retryWhen(observable -> observable.zipWith(retrySubject, (o, o2) -> o))
.subscribeWith(result -> {}, error -> {});
Your button will just emit an item from the Subject
:
retrySubject.onNext(EMPTY);
来源:https://stackoverflow.com/questions/47619115/how-to-retry-a-consumed-observable