我在使用Android上的RxJava从异步I / O读取时遇到问题。我创建了一个Flowable并订阅了一个发射器。它第一次工作,调用onNext然后onComplete完成,但是当我再次尝试相同的操作(创建一个新的Flowable和订阅)时,如果我故意抛出I / O错误,我会得到一个UndeliverableException。如果我执行emitter.isCancelled,则返回true。不知道为什么会这样。
Flowable getFlowable(){
Flowable.create(new FlowableOnSubscribe<Object>() {
@Override
public void subscribe(FlowableEmitter<Object> emitter) throws Exception {
getIOResponse(new IOListener() {
@Override
public void onInfo(Object ioResponse) {
emitter.onNext(ioResponse);
emitter.onComplete();
}
@Override
public void onError(Exception e) {
emitter.onError(e); //Throws UndeliverableException, emitter already cancelled
}
}
}
}
}
static void subscribe(){
disposable = getFlowable()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.timeout(TIME_OUT_SECONDS, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
.subscribe(new Consumer<Object>() {
@Override
public void accept(Object myObject) throws Exception {
someListener.onSuccess(myObject);
}
}, throwable -> {
someListener.onError(throwable);
}, () -> {
Log.d("****", "Completed");
});
}