SkipWhile 方法在 RxJava 中没有像我预期的那样工作
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")))
如果 isRetryButtonClicked
变量为 false 那么它不会打印 doOnNext 这没关系因为我告诉它在 isRetryButtonClicked
等于 false 时跳过,但是当用户点击重试按钮时我'会将 isRetryButtonClicked
值更改为 true,然后它必须开始打印 doOnNext 但当前结果仍然没有打印任何内容。
retryWhen
需要一个信号来从该内部源执行重试。但是,isRetryButtonClicked
是一个字段,因此对它的更改不是信号。你需要的是一个Subject
。例如:
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!");
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")))
如果 isRetryButtonClicked
变量为 false 那么它不会打印 doOnNext 这没关系因为我告诉它在 isRetryButtonClicked
等于 false 时跳过,但是当用户点击重试按钮时我'会将 isRetryButtonClicked
值更改为 true,然后它必须开始打印 doOnNext 但当前结果仍然没有打印任何内容。
retryWhen
需要一个信号来从该内部源执行重试。但是,isRetryButtonClicked
是一个字段,因此对它的更改不是信号。你需要的是一个Subject
。例如:
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!");