我在Completeable上使用retryWhen运算符,有没有办法告诉它从重试Flowable中完成? 像这样-
PublishSubject<?> retrySubject = PublishSubject.create();
public void someFunction() {
someCompletable.retryWhen(new Function<Flowable<Throwable>, Publisher<?>>() {
@Override
public Publisher<?> apply(Flowable<Throwable> throwableFlowable) throws Exception {
return throwableFlowable.flatMap(throwable -> retrySubject.toFlowable(BackpressureStrategy.MISSING));
}
}).subscribe();
}
public void ignoreError(){
retrySubject.onComplete();
}
答案 0 :(得分:1)
您不能通过将flatMap
设为空来停止它。同样,每个错误都会使越来越多的观察者订阅导致内存泄漏的主题。
使用takeUntil
通过另一个来源停止序列:
PublishProcessor<Throwable> stopProcessor = PublishProcessor.create();
source.retryWhen(errors ->
errors.takeUntil(
stopProcessor
)
.flatMap(error -> Flowable.timer(1, TimeUnit.SECONDS))
)
stopProcessor.onComplete();
编辑:如果要重复使用同一主题,可以在停止路径上取消显示项目:
PublishProcessor<Integer> stopProcessor = PublishProcessor.create();
source.retryWhen(errors ->
errors.takeUntil(
stopProcessor.ignoreElements().toFlowable()
)
.flatMap(error -> stopProcessor)
)
// retry
stopProcessor.onNext(1);
// stop
stopProcessor.onComplete();