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.
Example 1 -> Unsubscribe the subscribe
There is no need to unsubscribe in onCompleted
. Take a look at 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.
On the other hand, you definitely should unsubscribe in onDestroy
in order to prevent memory leaks.