我正在玩rxJava2并获得

io.reactivex.exceptions.ProtocolViolationException: Disposable already set!


尝试将内容添加到CompositeDisposable之后。运行该应用程序后,我调用loadPost(),当我调用loadPostFromRemoteDataStore()时,该应用程序崩溃。这是代码。

public class CountriesPresenterImpl extends MvpBasePresenter<CountriesView> implements CountriesPresenter {

@Inject AppRepository mAppRepository;

private final CompositeDisposable disposables = new CompositeDisposable();

private DisposableObserver observer = new DisposableObserver<List<Post>>() {

    @Override
    public void onNext(List<Post> posts) {
        getView().setData(posts);
        getView().showContent();
    }

    @Override
    public void onError(Throwable e) {
        getView().showError(e, false);
    }

    @Override
    public void onComplete() {

    }
};

@Override
public void attachView(CountriesView view) {
    super.attachView(view);
    App.getComponent().inject(this);
}

@Override
public void loadPost(boolean pullToRefresh) {
    disposables.add( mAppRepository.getPost()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.newThread())
            .subscribeWith(observer) );
}

@Override
public void loadPostFromRemoteDatatore(boolean pullToRefresh) {
    disposables.add( new AppRemoteDataStore().getPost()
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.newThread())
            .subscribeWith(observer) );
}

@Override
public void detachView(boolean retainInstance) {
    super.detachView(retainInstance);
    if (!retainInstance) {
        disposables.clear();
    }
}


}

最佳答案

好,

我得到了答案:


  
  与所有其他使用者一样,{@ code DisposableCompletableObserver}只能订阅一次。
  以后尝试将其订阅到新来源将产生一个
  {@link IllegalStateException}和消息{@code“ Disposable has set!”}。
  


https://github.com/ReactiveX/RxJava/blob/fa58d36375ecc084b21bca299ca4780946b15dc5/src/main/java/io/reactivex/observers/DisposableCompletableObserver.java

每次使用subscribeWith时,您都会创建一个DisposableObserver。

关于android - RxJava2“ProtocolViolationException:一次性已设置!”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43482263/

10-10 04:02