Better to use Ionic Lifecycle or Angular Lifecyle for Observable?


#1

Hi everyone ,

Wanted to know if Its better to use Ionic Lifecycle event or angular lifecycle for Observable ?
Example :

export class HomePage {

  private homeSubscriptions: Subscription = new Subscription();
  constructor(....) {

  }

  ionViewWillEnter()
  {
       this.homeSubscriptions.add(this.dataProvider.getFirstThing().subscribe(
                       data => {
                          console.log(data);
                       },
                       err => {
                          console.log(err);
                       },
                       () => {console.log("frist thing completed")}
                  );
        this.homeSubscriptions.add(this.dataProvider.getSecondeThing().subscribe(
                       data => {
                          console.log(data);
                       },
                       err => {
                          console.log(err);
                       },
                       () => {console.log("seconde thing completed");}
                  );
  }
 ionViewWillLeave(){
    this.homeSubscriptions.unsubscribe();
 }

or

export class HomePage implements OnInit, OnDestroy{

  private homeSubscriptions: Subscription = new Subscription();
  constructor(....) {

  }

  ngOnInit()
  {
       this.homeSubscriptions.add(this.dataProvider.getFirstThing().subscribe(
                       data => {
                          console.log(data);
                       },
                       err => {
                          console.log(err);
                       },
                       () => {console.log("frist thing completed")}
                  );
        this.homeSubscriptions.add(this.dataProvider.getSecondeThing().subscribe(
                       data => {
                          console.log(data);
                       },
                       err => {
                          console.log(err);
                       },
                       () => {console.log("seconde thing completed");}
                  );
  }
 ngOnDestroy(){
    this.homeSubscriptions.unsubscribe();
 }

thanks for any advice.

PS : To prevent memory leak with observable, I use a subscription that I unsubscribe after view leave.
But I read thie first comment about Angular : https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription

I tryied this one too and It’s unsubscribed when It should, like my previous one. But Which is better? I dont know…