简体   繁体   中英

SkipWhile method does not work as I expected in RxJava

boolean isRetryButtonClicked = false;

.retryWhen(throwableFlowable -> throwableFlowable.skipWhile(throwable -> !isRetryButtonClicked)
    .doOnNext(throwable -> Log.w("...", "doOnNext"))
    .doOnComplete(() -> Log.w("...", "doOnComplete"))
    .doOnError(throwable -> Log.w("...", "doOnError " + throwable))
    .doOnSubscribe(subscription -> Log.w("...", "doOnSubscribe")))

If the isRetryButtonClicked variable was false then it will not print doOnNext and that is okay because I told it to skip while isRetryButtonClicked equal false, But when the user clicks on the retry button I'll change the isRetryButtonClicked value to true, And after that it must start to print doOnNext but the current result is still not print anything.

retryWhen needs a signal to perform the retry from that inner source. However, isRetryButtonClicked is a field so changes to it are not signals. What you need is a Subject . For example:

PublishSubject<String> subjectToRetry = PublishSubject.create();

source.retryWhen(errors -> errors.zipWith(subjectToRetry, (e, s) -> s))
    .doOnNext(throwable -> Log.w("...", "doOnNext"))
    .doOnComplete(() -> Log.w("...", "doOnComplete"))
    .doOnError(throwable -> Log.w("...", "doOnError " + throwable))
    .doOnSubscribe(subscription -> Log.w("...", "doOnSubscribe")))

// inside the retryButton's click handler:

subjectToRetry.onNext("Doit!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM