Stack Diego
Stack Diego

Reputation: 1337

Android rxjava2 subscription within OnClick

I'm trying to dig into the wonders of the RxJava2 world, but I'm still pretty confused. Basically I have to call an API when the user clicks on a button, so I'm using a Retrofit2 client which returns an Observable that I subscribe on the on click method of the button.

The issue is that when the button is clicked twice I'll get:

io.reactivex.exceptions.ProtocolViolationException: It is not allowed to subscribe with a(n) <package>.MainActivity$1 multiple times. Please create a fresh instance of <package>.MainActivity$1 and subscribe that to the target source instead.

If I dispose the observer after the onComplete the api won't be called as the subscription is invalidated.. Am I missing/misunderstanding something?

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.button) Button button;

    private DisposableObserver<PopularGames[]> observer;
    private Observable<PopularGames[]> popularGamesObservable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        APIsInterface apiClient = MyApplication.getInstance(this).getApiClient();
        popularGamesObservable = apiClient.getPopularGames();

        observer = new DisposableObserver<PopularGames[]>() {

            @Override
            public void onNext(PopularGames[] result) {
                Timber.d("onNext " + Arrays.asList(result));
            }

            @Override
            public void onError(Throwable e) {
                Timber.e("onError " + e);
            }

            @Override
            public void onComplete() {
                Timber.d("onComplete");
            }
        };


    }


    @OnClick(R.id.button)
    public void onViewClicked() {
        popularGamesObservable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(observer);
    }
}

Upvotes: 0

Views: 1035

Answers (1)

Raghunandan
Raghunandan

Reputation: 133570

io.reactivex.exceptions.ProtocolViolationException is expected

What you can do

CompositeDisposable compositeDisposable = new CompositeDisposable();

Then

@OnClick(R.id.button)
public void onViewClicked() {
   compositeDisposable.add( popularGamesObservable.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeWith(new DisposableObserver<PopularGames[]>() {

        @Override
        public void onNext(PopularGames[] result) {
            Timber.d("onNext " + Arrays.asList(result));
        }

        @Override
        public void onError(Throwable e) {
            Timber.e("onError " + e);
        }

        @Override
        public void onComplete() {
            Timber.d("onComplete");
        }
    }));
}

Then in onDestory

compositeDisposable.dispose();

Upvotes: 1

Related Questions