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
.
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.
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.
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.
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
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With