Any counterpart for constructor? (Provider/Service)

Hi! Simple question, are there any counterpart for constructor? I need to clear some things to avoid leaks so I am looking for the function that gets called before the PROVIDER is destroyed.

It is ionViewWillUnload for Pages right? how about for Service/Providers?

Thanks.

Global providers are never destroyed.

how about? ngOnDestroy()? When the app is closed on background?

That’s a component life cycle hook. Providers aren’t components.

Platform has pause and resume events that might be what you are looking for.

So there are no way to clear my subscriptions contained inside a provider class? Thanks for the clarifications.

Of course there are.

subscription: Subscription = new Subscription();

startListening() {
  subscription = someObservable.subscribe(x => console.log(x));
}
stopListening() {
  subscription.unsubscribe();
}

No what I meant is before the app is closed, I want to clear all of my subscriptions by calling unsubscribe in each and every subscription, just to avoid leaks, or it doesn’t matter because…

I’ve read somewhere that:
“For other types of injectable (service, factory, provider) the concept of a destructor is probably not useful, since they are singletons which last for the duration of the app’s life.”

Call your stopListeners before logging out. If the app is closed forcefully by the user, the Observables stop. But you control the soft landing.

1 Like

Makes sense, thanks for the clarification.

1 Like

Thanks for this suggestion.