Can i call LifeCycle methods in Refresh function

Hello

I have a page on which list of people in the array will display if I removed any of them the array is getting updated to reflect that change I’m using DoRefresh function to refresh the page. To update the array i’m using several functions and I wrote them in the IonviewWillenter method. if i’m calling this ionviewillenter in my dorefresh function the list is getting updated but the previous values are not going . to remove the previous values what should i do or there is any other method of refreshing

doRefresh(event) {
    console.log('Begin async operation');
    setTimeout(() => {
            this.ionViewWillEnter();
      console.log('Async operation has ended');
      event.target.complete();
    }, 2000);
  }


 ionViewWillEnter() {
    this.followers();
    this.getuserfollowing();

    setTimeout(() => {
      console.log('set timeout');
      if (this.hasfollowers == true && this.hasfollowing == true) {
        this.getstats();
      }
    }, 1000);
  }

``
this is my code ![Screenshot (21)|690x388](upload://f4BCzJDXxaNlcLc7lu4Aq09jFa4.png)

How about making “wrapper” method which you will call in ionViewWillEnter and in doRefresh.

I tried wrapping all those in a single method but still the same issue i’m getting old values and new values on the page.

Not sure if I understood well… Your updated values aren’t getting reflected on template? In that case can you confirm that they get updated indeed (console log them after update)?

Just wondering why do you use setTimeouts here?

we are using asynchronous calls to the function that is the reason why we used setTimeout.

how can i call lifecycle methods manually in a refresh function ??

Here is the complete code if you want a brief understanding go through it

export class FollowersPage implements OnInit {

  follow: any = [];
  tasks: any = []; // followers array
  following: any = [];
  stats: any = [];
  isfollowing:any=[];
  hasfollowers:Boolean;
  hasfollowing:Boolean;
  nofollowers:Boolean;
  nofollowing:Boolean;
  

  constructor(public router: Router,
    public afAuth: AngularFireAuth,
    public store: AngularFireStorage,
    public data: AngularFireDatabase, ) { }

  ngOnInit() {   }

  ionViewWillEnter() {
    this.followers();
    this.getuserfollowing();
    setTimeout(() => {
      if (this.hasfollowers == true && this.hasfollowing == true) {
        this.getstats();
      }
      console.log(this.hasfollowing);
    console.log(this.hasfollowers);   
    }, 1000);
  }
 
  
 async  getuserfollowing(){
    const user = this.afAuth.auth.currentUser.uid;
    const dbref = this.data.database.ref('/following/');
    const snap = await dbref.once('value');           
    if (snap.child(user).exists()) {
      const notify = snap.child(user).val();
             this.following = (Object.values(notify)).reverse();
     console.log('following');
      console.log(this.following);
      this.hasfollowing=this.following.length!=0?true:false;
      console.log(this.hasfollowing);
    }
  }

  getstats() {
    console.log('getstats');
    let taskid = [];
    let folowingid = [];
    this.tasks.map(task => {
      taskid.push(task.id);
    });
    this.following.map(foll => {
      folowingid.push(foll.id)
    });
    taskid.map(task => {
      const test = (folowingid.includes(task));
      this.isfollowing.push(test);
    });
    console.log(this.isfollowing);
  }

  goBack() {
    this.router.navigate(['/profile'], { replaceUrl: true });
  }

  followers() {
    const databaseref = this.data.database.ref('/followers/' + this.afAuth.auth.currentUser.uid);
    databaseref.once("value", (snapshot) => {
      snapshot.forEach(child => {
        let values = (child.val());
        this.follow.push(values);
        console.log(this.follow);
        this.tasks = this.follow;
        console.log(this.tasks.length);
       this.hasfollowers=this.tasks.length!=0? true:false;
       console.log(this.hasfollowers);
       
      })
    })
  }

  async remove(i) {
    try {
      let user = this.afAuth.auth.currentUser.uid;
      let followerref = this.data.database.ref('/followers/' + user);
      const followerstats = await followerref.child(i).remove();
      let followingref=this.data.database.ref('/following/'+i);
      const followingstats=await followingref.child(user).remove();
      this.goBack();
    } catch (error) {
      console.log(error);
    }
  }

  async followback(i){
    console.log("followback");
    let reference1 = this.data.database.ref('/following/' + this.afAuth.auth.currentUser.uid);
    console.log(reference1);
    let followerid = i;
    let followingid = this.afAuth.auth.currentUser.uid;
    this.getuserdetails(followerid, 'following', followingid);
    this.getuserdetails(followingid, 'followers', followerid);
  }


  getuserdetails(id, node, nodeid) {
    let user = id;
    let name;
    let dbref = this.data.database.ref('/users/' + user);
    console.log(dbref);
    dbref.once('value', snap => {
      name = snap.val().fullname;
      console.log(name);
      let dataref = this.data.database.ref();
      let noderef = dataref.child('/' + node + '/' + nodeid + '/' + id)
      noderef.set({
        id: id,
        name: name,
      })
    })
  }


  doRefresh(event) {
    console.log('Begin async operation');
    this.goBack();  
    setTimeout(() => {
      console.log('Async operation has ended');
      event.target.complete();
    }, 2000);
  }
}

It’s not a whole lot of an exaggeration to say that “giving you a way to elegantly not do this” is one of the major reasons libraries like RxJS and frameworks like Angular even exist in the first place.

You virtually never want setTimeout in an Ionic app. You definitely never want setTimeout with a magic number, if for no other reason than you can never know if you’ve chosen the right one.

Other things I see here that are fraught with peril, should only be done if you know exactly what you are doing and why, and should be extensively commented to explain why you are deviating from normal procedure: the == operator (always use === instead) and explicit comparison against true instead of using implicit truthiness.

Finally, this post suggests what I would do instead of involving lifecycle events here at all.