Reputation: 3844
I'm playing with rxJava2 and getting
io.reactivex.exceptions.ProtocolViolationException: Disposable already set!
after trying to add stuff to CompositeDisposable. After running the app I'm calling loadPost() and when I'm calling loadPostFromRemoteDataStore(), the app crashes. This is the code.
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();
}
}
}
Upvotes: 1
Views: 1348
Reputation: 3083
You can't subscribe to two different observables with the same DisposableObservable instance. See the documentation here.
Upvotes: 1
Reputation: 4002
Ok,
I got the answer:
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!"}.
You have create an DisposableObserver every time you use subscribeWith.
Upvotes: 2