Search code examples
androidrx-javarx-android

RxJava make retryWhen() fire to onError() method


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

    }
}

Solution

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