RxJs difference between complete and unsubscribe in Observable?

*爱你&永不变心* 提交于 2019-12-22 08:30:01

问题


after complete event will unsubscribe Observable or not or any other difference.


回答1:


You complete an Observable, and unsubscribe a Subscription. These are two different methods on two different objects. You subscribe to an observable which returns a Subscription object.

If you want to stop listening to emits from the Observable you call subscription.unsubscribe().

If you want an Observable to be done with his task, you call observable.complete(). (this only exists on Subject and those who extend Subject). The complete method in itself will also unsubscribe any possible subscriptions.

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.




回答2:


If you complete an Observable, it will call complete() method then the teardown logic and unsubscribe(). Calling unsubscribe() itself does not call complete method. Angular async pipe is an example of calling unsubscribe. Therefore, if you have complete method and using async pipe, it will not be called.



来源:https://stackoverflow.com/questions/52198240/rxjs-difference-between-complete-and-unsubscribe-in-observable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!