0

Yes, I have search and read lots of discuss over internet but all of them does not answer my question!!!

Angular/RxJs When should I unsubscribe from `Subscription`

Angular 2 Subscribe / Unsubscribe Observables in case of http calls

My quest is: angular (2+) http-response stream, do I need to unsubscribe after or before I re-use it? Say, I have :

  private mystream$: Subscription;

  public MyTask(){
    if (this.mystream$){ this.mystream$.unsubscribe }
    this.mystream$ = http.get(url).subscribe({
      // to do
    })
  }


  ngOnDestroy() {
    if (this.mystream$) { this.mystream$.unsubscribe(); }
  }

All I have read, this unsubscribe onDestroy and stream variable check before use is not necessary, but I haven't being convinced yet, as my http.put call over internet , there is no warrant, it will be or must be complete before application to call MyTask() again.

Please tell me why I am wrong and this http.put stream check before use and clear up is not necessary, even call over internet and my user is not a patient one.

Sonicd300
  • 1,950
  • 1
  • 16
  • 22
Long Field
  • 858
  • 9
  • 16
  • Just realise that rxjs or observable don't have "cancel token" function that can fix my worry – Long Field Oct 08 '17 at 10:09
  • So, shareReplay() of httpclient should put my worry away – Long Field Oct 12 '17 at 00:14
  • Possible duplicate of [Is it necessary to unsubscribe from observables created by Http methods?](https://stackoverflow.com/questions/35042929/is-it-necessary-to-unsubscribe-from-observables-created-by-http-methods) – Mouneer Aug 08 '18 at 09:49

1 Answers1

0

This answer is just for the case you have mentions, i.e a http request for a get method.

In the official site of angular https://angular.io/guide/observables you ll find a sentence written like this "The subscribed consumer then receives notifications until the function completes, or until they unsubscribe.". It means the subscribe method got unsubscribe by angular itself when it gets completed.

To execute the observable you have created and begin receiving notifications, you call its subscribe() method, passing an observer. This is a JavaScript object that defines the handlers for the notifications you receive. The subscribe() call returns a Subscription object that has an unsubscribe() method, which you call to stop receiving notifications.

The above explanation shows that you have an extra advantage of unsubscribe() method to unsubscribe you subscription according to ur conditions. but it's never mandatory to unsubscribe before using it again.

Now when i come to your code you are unsubscribing it in ngOnDestroy which says "This is the time to notify another part of the application that the component is going away.".Already all the ongoing subscriptions related to this component is aborted by angular with the change in component. So There is no meaning of asking to unsubscribe in the ngOnDestroy() method.

And if i ll focus on the http method, this is the explaination found in the [https://angular.io/guide/http].

An HttpClient method does not begin its HTTP request until you call subscribe() on the observable returned by that method. The AsyncPipe subscribes (and unsubscribes) for you automatically.

The async pipe subscribes to an Observable or Promise and returns the latest value it has emitted. When a new value is emitted, the async pipe marks the component to be checked for changes. When the component gets destroyed, the async pipe unsubscribes automatically to avoid potential memory leaks.

Some common cases where we use unSubscribe() usually

  • To clear the timeout to stop execution
  • Remove something from the observers array so it will no longer be notified and many more.

But yes, In some cases you may need to unsubscribe your subscription in the ngOnDestroy() method. But not needed here. Go through all the examples from the link i have provided. It may help you better. Hope i helped a bit.

Abhiz
  • 970
  • 1
  • 16
  • 36