RxJava在onSubscribe()返回一个导致NullPointerException的订阅之前,可以在Observable.empty()调用onComplete()上订阅()吗?

时间:2015-06-20 00:51:25

标签: java observable rx-java

我写的逻辑将两个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;
}

0 个答案:

没有答案