何时取消订阅 [英] When to unsubscribe a subscription

查看:78
本文介绍了何时取消订阅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有关如何取消订阅可观察对象的问题.我有两个代码,但我不确定哪一个更好.

I have a question regarding how to unsubscribe an observable. I have two codes and I'm not really sure about which one is better.

示例1->流结束后,退订订户:

Example 1 -> Unsubscribe the subscriber once the stream has finished:

Subscriber<String> subscriber = new Subscriber<String>() {
        @Override
        public void onCompleted() {
            progressdialog.dissmiss();
            unsubscribe();
        }

        @Override
        public void onError(Throwable e) {
            progressdialog.dissmiss();
        }

        @Override
        public void onNext(String s) {
            // do something with data
        }
    }

示例2->活动被破坏后,取消订阅:

Example 2 -> Unsubscribe the subscription once the activity is destroyed:

private void test(){
    Subscriber<String> subscriber = new Subscriber<String>() {
        @Override
        public void onCompleted() {
            progressdialog.dissmiss();
        }

        @Override
        public void onError(Throwable e) {
            progressdialog.dissmiss();
        }

        @Override
        public void onNext(String s) {
            // do something with data
        }
    };

    subscription = BackendRequest.login(loginRequest)
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(subscriber);

    compositeSubscription.add(subscription);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    this.subscription.unsubscribe();
}

我不得不提到我的可观察对象只会发出一次,该活动不应等待来自可观察对象的更多调用.

I have to mention that my observables only will emit once, the activity should not wait for more calls from the Observable.

哪个更好?

预先感谢

推荐答案

从这两个选项中,第二个更好.

From the two options the second one is better.

在第一个示例中,您不需要 onComplete()方法中的取消订阅.如果您到达订阅的 onComplete(),则不再有取消订阅的责任.

In your first example you're unsubscribing in the onComplete() method which is not needed. If you reach the onComplete() of a Subscription you don't have the responsibility of unsubscribing from it anymore.

您的第二个例子是正确的例子. CompositeSubscription 背后的想法是,您可以向其中添加多个 Subscriptions ,然后立即清理( unsubscribe ).换句话说,这使您不必保留需要取消订阅的 Subscriptions 列表.

Your second example is the correct one. The idea behind the CompositeSubscription is that you can add multiple Subscriptions to it and then clean up (unsubscribe) at once. In other words this just saves you from the need of keeping a list of Subscriptions that you need to unsubscribe from.

使用 CompositeSubscription 的一个棘手的部分是,如果您取消订阅,则可以再次使用它.您可以查看 compositeSubscription.add()的文档方法以了解详细信息.简而言之-它会直接取消订阅您要添加的订阅.这是一个深思熟虑的决定(您可以在 HERE 中了解更多信息).

One tricky part using CompositeSubscription is that if you once unsubscribe it, you can NOT use it again. You can check the documentation for the compositeSubscription.add() method for details why. In short - it will directly unsubscribe the Subscription you're trying to add. That's been a deliberate decision (you can read more about it HERE).

回到您的示例,在Activity的 onDestroy()中调用 unsubscribe()很好,这样可以避免内存泄漏.关于您的评论,当您多次调用 test()方法时会出现问题-我想说您的问题在其他地方.也许您的用例不允许多次调用它,也许您应该在使用新收到的数据之前清除旧数据,等等.也许如果您已详细说明了您将面对的问题,我们将为您提供更多帮助.但是就 CompositeSubscription 而言-您正在使用它,并且已正确取消订阅它!

Coming back to your example, calling unsubscribe() in onDestroy() of the Activity is fine and will save you from memory leaks. Regarding your comment that problems occur when you call your test() method multiple times - I'd say your problem is somewhere else. Maybe your use-case shouldn't allow to call it multiple times, maybe you should cleanup old data before using the newly received one, etc. Perhaps if you have explained in details what kind of problems you face we could help more. But as far as the CompositeSubscription is concerned - you're using it and unsubscribing from it correctly!

这篇关于何时取消订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆