何时取消订阅
When to unsubscribe a subscription
我有一个关于如何取消订阅 observable 的问题。我有两个代码,我不确定哪个更好。
示例 1 -> 流结束后取消订阅订阅者:
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 -> activity 被销毁后取消订阅:
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();
}
我不得不提一下,我的 Observable 只会发出一次,activity 不应该等待来自 Observable 的更多调用。
哪个更好?
提前致谢
我认为这取决于您的需求。如果 activity 不等待任何其他调用,我想您可以在 onCompleted() 中取消订阅。
我总是在 onDestroy()
中退订
@Override
protected void onDestroy() {
super.onDestroy();
if (subscription != null) {
subscription.unsubscribe();
}
}
编辑:看看 http://reactivex.io/RxJava/javadoc/rx/subscriptions/CompositeSubscription.html
private CompositeSubscription mCompositeSubscription = new CompositeSubscription();
private void doSomething() {
mCompositeSubscription.add(
AndroidObservable.bindActivity(this, Observable.just("Hello, World!"))
.subscribe(s -> System.out.println(s)));
}
@Override
protected void onDestroy() {
super.onDestroy();
mCompositeSubscription.unsubscribe();
}
onCompleted
无需退订。看看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 subscriptions that are ended by the
Observable in this way.
另一方面,您绝对应该在 onDestroy
取消订阅,以防止内存泄漏。
从两个选项中,第二个更好。
在您的第一个示例中,您在 onComplete()
方法中 unsubscribing
,这不是必需的。如果您达到订阅的 onComplete()
,您不再有取消订阅的责任。
你的第二个例子是正确的。 CompositeSubscription
背后的想法是,您可以向其中添加多个 Subscriptions
,然后一次清理 (unsubscribe
)。换句话说,这只是让您无需保留需要取消订阅的 Subscriptions
列表。
使用 CompositeSubscription
的一个棘手部分是,如果你曾经 unsubscribe
它,你可以 NOT 再次使用它。您可以查看 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
而言 - 您正在使用它并正确取消订阅!
我有一个关于如何取消订阅 observable 的问题。我有两个代码,我不确定哪个更好。
示例 1 -> 流结束后取消订阅订阅者:
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 -> activity 被销毁后取消订阅:
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();
}
我不得不提一下,我的 Observable 只会发出一次,activity 不应该等待来自 Observable 的更多调用。
哪个更好?
提前致谢
我认为这取决于您的需求。如果 activity 不等待任何其他调用,我想您可以在 onCompleted() 中取消订阅。
我总是在 onDestroy()
中退订@Override
protected void onDestroy() {
super.onDestroy();
if (subscription != null) {
subscription.unsubscribe();
}
}
编辑:看看 http://reactivex.io/RxJava/javadoc/rx/subscriptions/CompositeSubscription.html
private CompositeSubscription mCompositeSubscription = new CompositeSubscription();
private void doSomething() {
mCompositeSubscription.add(
AndroidObservable.bindActivity(this, Observable.just("Hello, World!"))
.subscribe(s -> System.out.println(s)));
}
@Override
protected void onDestroy() {
super.onDestroy();
mCompositeSubscription.unsubscribe();
}
onCompleted
无需退订。看看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 subscriptions that are ended by the Observable in this way.
另一方面,您绝对应该在 onDestroy
取消订阅,以防止内存泄漏。
从两个选项中,第二个更好。
在您的第一个示例中,您在 onComplete()
方法中 unsubscribing
,这不是必需的。如果您达到订阅的 onComplete()
,您不再有取消订阅的责任。
你的第二个例子是正确的。 CompositeSubscription
背后的想法是,您可以向其中添加多个 Subscriptions
,然后一次清理 (unsubscribe
)。换句话说,这只是让您无需保留需要取消订阅的 Subscriptions
列表。
使用 CompositeSubscription
的一个棘手部分是,如果你曾经 unsubscribe
它,你可以 NOT 再次使用它。您可以查看 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
而言 - 您正在使用它并正确取消订阅!