Reputation: 5637
I have a following class with RxJava implementation for download two resources from API. I do some rx to allow it retry or repeat when it don't meet api/connection requirements. However, I cannot make retryWhen() fire onError() after attempt more than 3 times.
QUESTION / HELP
please, review my code and help me solve this issue.
NOTE
I'm implement Rx by read these two articles. 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() {
}
}
Upvotes: 2
Views: 681
Reputation: 2420
Pass the retry counter and error into a Pair
object if you've reached the retry limit get the exception from pair object and raise it.
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));
Upvotes: 0
Reputation: 21
To retain the throwable after retrying (instead of emitting a custom one), return an Observable with the appropriate error from the zipWith operator when retryCount is over the specified limit.
.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);
});
Upvotes: 2
Reputation: 1459
When I wrote similar code before, I had manually threw Observable.error()
in flatMap
.flatMap(retryCount -> {
if (retryCount >= STOP_RETRY_TIME) {
return Observable.error(someException);
}
return Observable.timer(RETRY_TIMEOUT, TimeUnit.SECONDS);
}))
Upvotes: 0