Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do we need to unsubscribe from observable that completes/errors-out?

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?

like image 225
Titan Avatar asked Dec 26 '16 18:12

Titan


1 Answers

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

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:

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.

like image 52
cartant Avatar answered Nov 10 '22 09:11

cartant