我写的逻辑将两个Observable合并在一起,这样我就可以确保它们一起运行并完成一个整体。为此,我使用了Observable.mergeDelayError()
,如下所示。但是,当我编写单元测试时,有时候,当我尝试调用NullPointerException
mRefreshSubscription.unsubscribe()
为空时,我会得到mRefreshSubscription
。
问题:
生成的合并Observable是否可能是双空的,继续调用onComplete()
比subscribe()
更快地返回订阅?
Observable observA = Observable.empty();
Observable observB = Observable.empty();
mRefreshSubscription = Observable.mergeDelayError(observA, observB)
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(getRefreshObserver());
private Observer getRefreshObserver() {
if (mRefreshObserver == null) {
mRefreshObserver = new Observer() {
@Override
public void onCompleted() {
mRefreshSubscription.unsubscribe();
}
@Override
public void onError(Throwable e) {
mRefreshSubscription.unsubscribe();
}
@Override
public void onNext(Object o) {
// do nothing
}
};
}
return mCleanAndAutoSubscriberObserver;
}