本文介绍了如何重试消耗的Observable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重新执行失败的已定义可观察对象.我想结合使用Retrofit2和RxJava2,然后单击按钮时重试特定请求及其订阅和行为.那可能吗?

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(....)

推荐答案

一个选项是创建Publisher,该控件的发射由您的按钮控制.

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 -> {});

您的按钮只会从Subject发射一个项目:

Your button will just emit an item from the Subject:

retrySubject.onNext(EMPTY);

这篇关于如何重试消耗的Observable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 00:37