gpt4 book ai didi

android - RxJava2 "ProtocolViolationException: Disposable already set!"

转载 作者:行者123 更新时间:2023-11-29 16:59:35 30 4
gpt4 key购买 nike

我正在玩 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();
}
}

最佳答案

好的,

我得到了答案:

  • Like all other consumers, {@code DisposableCompletableObserver} can be subscribed only once.

  • Any subsequent attempt to subscribe it to a new source will yield an
  • {@link IllegalStateException} with message {@code "Disposable already set!"}.

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

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

关于android - RxJava2 "ProtocolViolationException: Disposable already set!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43482263/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com