如何在Android上的RxJava 2.0中部署Disposable?

时间:2017-05-02 11:35:26

标签: android disposable

在下面的代码Function GetBEnabled(ByVal control As Office.IRibbonControl) As Boolean Return True End Function 中,如果mSafeObserver.isDisposed();isDatabaseEmpty == false未被调用),则始终返回空指针异常。

zipped.subscribe(mSafeObserver);

在rxJava 1.0中,这段代码是正确的:

private SafeObserver<ResponseHelper> mSafeObserver; 

private Observer<ResponseHelper> observer = new Observer<ResponseHelper>() {
    // ...
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSafeObserver = new SafeObserver<>(observer);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // ...        
    if (isDatabaseEmpty) {
        zipped.subscribe(mSafeObserver);
    } else {
        initRecycler();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mSafeObserver != null && !mSafeObserver.isDisposed()) 
        mSafeObserver.dispose();
}

如何检查我是否可以处置Disposable?

1 个答案:

答案 0 :(得分:0)

我不知道压缩指的是什么,但我会这样做

private CompositeDisposable mDisposable = new CompositeDisposable();
private Observer<ResponseHelper> observer = 
new Observer<ResponseHelper>() {
    // ...
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mSafeObserver = new SafeObserver<>(observer);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // ...        
    if (isDatabaseEmpty) {
        zipped
            .doOnSubscribe(mDisposable::add)
            .subscribe(mSafeObserver);
    } else {
        initRecycler();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    mDisposable.dispose();
}