Saving subscription to unsubscribe?


#1

I have a question about subscription. Do i have to save subscription and unsubscribe later? For example when i subscribing in page constructor to subject from my provider?


#2

I use the async pipe for that. Makes debugging easier, because Angular unsubscribes automatically. It takes some practice, but it’s worth it imo.


#3

One thing I’m doing for Observables that don’t wind up directly in the template is introduce a subject:
private lifetime = new Subject<void>();

And use takeWhile on those Observables:

mySweetObservable
.takeUntil(this.lifetime)
.subscribe(value => this.tubularVariable => value * 9001);

Then in ionViewWillLeave do:

  ionViewWillLeave() {
    this.lifetime.next();
    this.lifetime.complete();
  }

edit
Although, having just read the documentation both of those lines may be unnecessary, and you might be able to just use this.lifetime.complete.