Rxjava: Is unsubscribing a composite subscription better or clearing it?

Created on 27 Apr 2016  路  12Comments  路  Source: ReactiveX/RxJava

Question

Most helpful comment

If you know which one, you can use remove() for that. It will remove and unsubscribe that particular Subscription.

All 12 comments

If you add/remove Subscriptions to/from it according to a lifecycle (like onCreate starts all your Observables and onDestroy stops them), you can simply use clear. However, if some off-thread task keeps starting your Observables, you need unsubscribe and replacement of the entire CompositeSubscription to prevent latecommers to keep runnning.

@akarnokd If we have a composite subscription , out of which we want only few subscriptions to be unsubscribed (all were added/removed according to lifecycle) , should we individually unsubscribe each of them or can we still use composite subscription?

If you know which one, you can use remove() for that. It will remove and unsubscribe that particular Subscription.

I'm closing this issue due to inactivity. If you have further input on the issue, don't hesitate to reopen this issue or post a new one.

@akarnokd I just took a look of the source code of clear and unsubscribe. The only difference between them is that unsubscribe method makes this.unsubscribed = true so you can't add subscription to it later. But as we know the Rxjava can lead to memory leak for the ActionSubscriber holds reference of onNext and onError even if you unsubscribe it. So people sugguest me to use clear method instead of unSubscribe because clear method can clear reference to the onNext and onError. But I didn't get it from the source code. Could you figure it out for me ?

Both remove references and prevent leaks. Clear lets you reuse the composite though when the Activity is resumed/recreated.

Do you mean that both the clear and unsubscribe remove references and prevent leaks? But recently I always get a lot of Rxjava memory leak even if I have used the unsubscribe method. You can try to read this article.

What is your code? Don't hold references to the individual Disposables.

`
public class Presenter {
private Subscription subscription = Subscriptions.empty();

public Presenter(RecommendMovieUseCase recommendMovieUseCase) {
    this.recommendMovieUseCase = recommendMovieUseCase;
}

private void showRecommendedMovieTitle(final MovieSuggestionView view) {
    view.showProgress();
    subscription = recommendMovieUseCase.recommendRandomMovie()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<String>() {
                @Override
                public void call(String movieTitle) {
                    view.hideProgress();
                    view.showTitle(movieTitle);
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                    view.hideProgress();
                    view.showLoadingError();
                }
            });
}

public void destroy() {
    subscription.unsubscribe();
    view = null;
}

}
`

It's just like this.

Null out the subscription field.

you mean that I have to set subscription = null wherever I use it ?

Yes.

Was this page helpful?
0 / 5 - 0 ratings