0

If we have an @injectable singleton service with constructor injection that works like this:

constructor(private ds:DataService) {

   ds.obervable$.subscribe(data=>...)

}

Do we need to unsubscribe. It seems like we don't because it's a Singleton service, and we are only creating one subscription in this scenario, and it will get destroyed when the service gets destroyed, which is when the Application shuts down.

Did I think about that right?

Ole
  • 41,793
  • 59
  • 191
  • 359
  • 1
    You could have a look at [this question](https://stackoverflow.com/questions/61104129/is-it-necessary-to-unsubscribe-complete-in-a-backend-service-to-prevent-side-eff/61126488#61126488) and [this answer](https://stackoverflow.com/a/61126488/9632621) – Andrei Gătej May 02 '20 at 09:13

2 Answers2

1

It won't be destroyed when the service gets destroyed. You must do it manually, and there is no such thing such as OnDestroy hook on the service. However, it is usually alright because the singleton is always there when the application is on. The only case where you need to unsubscribe is when you do not need the service anymore (for exemple: replace the provider by another object for the same provider).

HTN
  • 3,388
  • 1
  • 8
  • 18
  • 2
    A service can have `ngOnDestroy`. [SO answer](https://stackoverflow.com/a/61126488/9632621). [Live demo](https://ng-run.com/edit/7r4rpUZfGxsyfOaepHuI?open=app%2Ffoo.service.ts) – Andrei Gătej May 02 '20 at 09:15
  • Here's how I thought about it. The service is a Singleton. When it subscribes it will only ever create one subscription. That subscription gets destroyed when the application shuts down. So there's is really no need to unsubscribe. Did I miss anything? I think we are saying the same thing. No need to unsubscribe in singleton services....assuming they will only be created once within the application. – Ole May 02 '20 at 17:27
  • Services can implement `OnDestroy` and the hook is called. – Ole May 02 '20 at 17:27
0

I believe there is some confusion here between the lifecycle of the service and the lifetime of a subscription to an observable that is returned by that service.

While it is true that, if we only subscribe once to an observable, there will only be one subscription, but this has nothing whatsoever to do with whether or not the service is a singleton. The subscription does not belong to the service, it belongs to the consumer of the service.

However, it is incorrect to consider the lifetime of the subscription as tied to the lifetime of the service. They have entirely independent lifetimes.

When the application shuts down, both will be destroyed but only because they are both part of that application.

Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
  • Could you elaborate a bit on `incorrect to consider the lifetime of the subscription as tied to the lifetime of the service` ? I believe this holds true when the service is singleton(as you already mentioned), but when a service is not provided at root level(e.g at comp level), if the subscription is not unsubscribed when the serv. gets destroyed, one might run into mem. leaks. Aren't these entities somehow tied? – Andrei Gătej May 02 '20 at 09:22
  • In the context I provided (And I mean this context very strictly) there will be only one subscription ever created by the constructor of the service. The remaining assumption is that the service lives for the lifetime of the application. When the application shuts down the service is destroyed, and so is the subscription. So unsubscribing from it is redundant. Did I miss anything? – Ole May 02 '20 at 17:31
  • You didn't explain your question that you're subscribing inside a service but it wouldn't matter because you don't save a reference to the subscription in that service or anywhere else. Therefore what I said is still applicable. However you should have phrased your question more specifically – Aluan Haddad May 03 '20 at 16:04