虽然其中一个轨道被发射onError()
,但另一个轨道中的isCancelled()
仍然返回false,从而导致UndeliverableException
。如何检查下游在并行导轨中被取消?
Disposable disposable = Flowable.create(new FlowableOnSubscribe<Integer>() {
@Override
public void subscribe(FlowableEmitter<Integer> emitter) throws Exception {
System.out.println("Flowable.create-emitter.isCancelled:" + emitter.isCancelled());
for (int i = 1; i < 10; i++) {
emitter.onNext(i);
}
emitter.onComplete();
}
}, BackpressureStrategy.BUFFER).parallel(6).runOn(Schedulers.io())
.flatMap(new Function<Integer, Publisher<String>>() {
@Override
public Publisher<String> apply(Integer t) throws Exception {
// TODO Auto-generated method stub
return Flowable.create(new FlowableOnSubscribe<String>() {
@Override
public void subscribe(FlowableEmitter<String> emitter) throws Exception {
System.out.println("flatMap-before onError-isCancelled:" + emitter.isCancelled());
try {
if (true) { // assume trigger the error
throw new Exception("Test");
}
if (!emitter.isCancelled()) {
emitter.onNext(String.valueOf((t + 1)));
emitter.onComplete();
}
} catch (Exception ex) {
if (!emitter.isCancelled()) {
emitter.onError(ex);
}
}
System.out.println("flatMap-after onError-isCancelled:" + emitter.isCancelled());
}
}, BackpressureStrategy.BUFFER);
}
}).sequential().subscribeOn(scheduler).observeOn(Schedulers.single())
.subscribeWith(new ResourceSubscriber<String>() {
public void onComplete() {
System.out.println("onComplete");
}
public void onError(Throwable arg0) {
System.out.println("onError:" + arg0.toString());
}
public void onNext(String arg0) {
System.out.println("onNext:" + arg0);
}
});
答案 0 :(得分:1)
我找到了解决方案。我需要添加一个全局错误消费者来解决问题。 https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling
答案 1 :(得分:0)
您呼叫onError()
的瞬间整个下游观察链将完成。之后您无法执行onNext()
,onError()
或onComplete()
,因为无法提供任何内容。这被称为“被动合同”。