我们是否需要取消订阅完整的/出错的可观察项目? [英] Do we need to unsubscribe from observable that completes/errors-out?

查看:83
本文介绍了我们是否需要取消订阅完整的/出错的可观察项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我知道观察对象一定会完成(通过completeerror通知)之前我的组件/类超出范围,我是否仍需要取消订阅防止内存泄漏?换句话说,是否可以自动完成/出错的可观察清理,所以我不必担心?

When I know that the observable will definitely complete (either with complete or an error notification) before my component/class goes out of scope, do I still need to unsubscribe from it to prevent memory leaks? In other words, is completed/error-ed observable cleaned-up automagically so I don't have to worry?

推荐答案

订阅和取消订阅部分rel ="noreferrer"> 可观察的合同 是关于您的问题的权威.它指出:

The Subscribing and Unsubscribing section of the Observable Contract is definitive regarding your question. It states:

当Observable向其观察者发出OnError或OnComplete通知时,这将终止订阅.观察者无需向以这种方式终止的,由Observable终止的订阅发出取消订阅通知.

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.

可观察的终止部分中也提到了这一点:

This is also mentioned in the Observable Termination section:

当Observable向其观察者发出OnError或OnComplete通知时,这将终止订阅.

When an Observable issues an OnError or OnComplete notification to its observers, this ends the subscription.

因此,不,无需取消订阅已完成或存在错误的可观察对象.但是,这样做没有害处.

So, no, there is no need to unsubscribe from observables that complete or error. However, there is no harm in doing so.

这篇关于我们是否需要取消订阅完整的/出错的可观察项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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