我有一个带有RxJava实现的以下类,用于从API下载两个资源。我做了一些rx,允许它在不符合api /连接要求时重试或重复。但是,在尝试超过3次之后,我无法使retryWhen()触发onError()。
问题/帮助
请查看我的代码并帮我解决这个问题。
注意
我通过阅读这两篇文章来实现Rx。 article 1,article 2
SplashPresenter.class
public class SplashPresenter implements SplashContract.Presenter {
private static final String TAG = SplashPresenter.class.getName();
private static final int RETRY_TIMEOUT = 10;
private static final int STOP_RETRY_TIME = 3;
private static final int START_RETRY_TIME = 1;
private SplashContract.View mView;
@Override
public void init(SplashContract.View view) {
this.mView = view;
}
@Override
public void onResume() {
GetRemoteReceiverRelationshipSpec relationSpec = new GetRemoteReceiverRelationshipSpec();
GetRemoteIncompleteReasonSpec reasonSpec = new GetRemoteIncompleteReasonSpec();
Observable<RepoResult<ArrayList<IncompleteReasonViewModel>>> queryReason =
Repository.getInstance().query(reasonSpec);
Repository.getInstance().query(relationSpec)
.concatMap(result -> queryReason)
.repeatWhen(complete -> complete
.zipWith(Observable.range(START_RETRY_TIME, STOP_RETRY_TIME), (v, i) -> i)
.flatMap(repeatCount -> {
Log.i(TAG, "Repeat attempt: " + repeatCount);
mView.showLoadingDialog();
return Observable.timer(RETRY_TIMEOUT,
TimeUnit.SECONDS);
}))
.takeUntil(RepoResult::isSuccess)
.retryWhen(error -> error
.zipWith(Observable.range(START_RETRY_TIME, STOP_RETRY_TIME), (v, i) -> i)
.flatMap(retryCount -> {
Log.i(TAG, "Retry attempt: " + retryCount);
mView.showLoadingDialog();
if (mView.getCommunicator() != null) {
mView.getCommunicator().onConnectionFail(retryCount);
}
return Observable.timer(RETRY_TIMEOUT,
TimeUnit.SECONDS);
}))
.filter(RepoResult::isSuccess)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
result -> Log.i(TAG, "onNext()"),
err -> {
Log.i(TAG, "onError()");
if (mView.getCommunicator() != null) {
mView.dismissLoadingDialog();
mView.getCommunicator().onSplashScreeDismissError();
}
},
() -> {
Log.i(TAG, "onComplete()");
if (mView.getCommunicator() != null) {
mView.dismissLoadingDialog();
mView.getCommunicator().onSplashScreenSuccessDismiss();
}
}
);
}
@Override
public void onPause() {
}
}
答案 0 :(得分:1)
要在重试后保留throwable(而不是发出自定义的),当retryCount超过指定的限制时,从zipWith运算符返回带有相应错误的Observable。
.retryWhen(error -> {
Observable<Integer> range = Observable.range(START_RETRY_TIME, STOP_RETRY_TIME);
Observable<Observable<Long>> zipWith = error.zipWith(range, (e, i) ->
i < STOP_RETRY_TIME ?
Observable.timer(i, TimeUnit.SECONDS) :
Observable.error(e));
return Observable.merge(zipWith);
});
答案 1 :(得分:0)
之前我写过类似的代码时,我手动将Observable.error()
放在flatMap
.flatMap(retryCount -> {
if (retryCount >= STOP_RETRY_TIME) {
return Observable.error(someException);
}
return Observable.timer(RETRY_TIMEOUT, TimeUnit.SECONDS);
}))
答案 2 :(得分:0)
如果已达到重试限制,则将重试计数器和错误传递给Pair
对象,请从对对象获取异常并引发它。
source.retryWhen(errors -> errors
.zipWith(Observable.range(1, REQUEST_RETRY), Pair::new)
.flatMap(errPair -> {
int retryTime = errPair.second;
if (retryTime < REQUEST_RETRY) {
return Observable.timer(retryTime * RETRY_DELAY, TimeUnit.MILLISECONDS);
} else {
return Observable.error(errPair.first);
}
})
.doOnError(this::handleError));