Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it recommended to call Disposable.dispose() as soon as a subscription completes work?

I have an Activity in which I am creating and subscribing to multiple instances of the Single class (each instance is doing some work in a separate background thread). For each subscription, I'm adding the Disposable instance that's created to a CompositeDisposable instance which is scoped to the Activity. When the Activity is destroyed, I am calling the CompositeDisposable.clear() method to dispose of all subscriptions in the Activity. This, of course, means that all Disposable instances (including those for subscriptions which have completed work) hang around in my Activity until the Activity is destroyed.

Is this okay or should I call Disposable.dispose() for each individual subscription every time the particular Single instance completes work (i.e. when the SingleObserver receives the onSuccess or onError callback)? The problem with the latter approach is that I have to keep track of which Disposable links to which SingleObserver, and this defeats the point of using CompositeDisposable.

like image 560
Adil Hussain Avatar asked Jun 18 '17 12:06

Adil Hussain


People also ask

When to Dispose is called in RxJava?

You should call dispose() in onStop() instead of onDestroy(). Image a scenario where just after making the API call, your activity goes into the background. The activity might not have been destroyed at that time so you won't dispose the disposable.

What is dispose in RxJava?

RxJava 2 introduced the concept of Disposables . Disposables are streams/links/connections between Observer and Observable . They're meant to give power to the Observers to control Observables . In most cases, the Observer is a UI element, ie; Fragment , Activity , or a corresponding viewmodel of those elements.


2 Answers

No, you shouldn't. When an Observable is completed, Observable is disposed of by itself. This is part of the Observable contract:

When an Observable issues an OnError or OnComplete notification to its observers, this ends the subscription. Observers do not need to issue an Unsubscribe notification to end the subscriptions that are ended by the Observable in this way.

like image 128
yosriz Avatar answered Sep 17 '22 14:09

yosriz


Yosriz's answer is the correct answer but if you do want remove a Disposable instance from your CompositeDisposable instance as soon as the Single subscription completes work (and not have the Disposable instance hang around), then I've put together the following abstract class:

public abstract class MySingleObserver<T> implements SingleObserver<T> {

    private Disposable disposable;

    @Override
    public void onSubscribe(@NonNull Disposable disposable) {
        this.disposable = disposable;
        onStart();
    }

    public Disposable getDisposable() {
        return disposable;
    }

    public abstract void onStart();
}

You can pass concrete extensions of this class in as the SingleObserver of your Single subscriptions as follows:

Single.just(1)
      .subscribe(new MySingleObserver<Integer>() {

          @Override
          public void onStart() {
              MyActivity.this.myCompositeDisposable.add(getDisposable());
              // do something further
          }

          @Override
          public void onSuccess(@NonNull Integer success) {
              MyActivity.this.myCompositeDisposable.remove(getDisposable());
              // do something further
          }

          @Override
          public void onError(@NonNull Throwable error) {
              MyActivity.this.myCompositeDisposable.remove(getDisposable());
              // do something further
          }
      });
like image 34
Adil Hussain Avatar answered Sep 17 '22 14:09

Adil Hussain