When to unsubscribe a subscription

前端 未结 3 1976
清酒与你
清酒与你 2021-02-03 20:54

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

3条回答
  •  被撕碎了的回忆
    2021-02-03 21:21

    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.

提交回复
热议问题