Mateus Neves
Mateus Neves

Reputation: 764

How to automatically update information changed in Firestore

I have this code to display username that was stored in Firestore when signing up:

   func displayUserName(){
    let db = Firestore.firestore()
    
    if let uid = user?.uid{
        
        db.collection("PROFILE").document(uid).getDocument { (snap, err) in
            
            guard let data = snap else {return}
            
            let firstname = data.get("firstName") as! String
            
            self.firstName = firstname
            
        }
       
    }
   
}

but, when I change the name in Firestore, I need to relaunch the app so it can update. is it possible to update this name without needing to relaunch the app?

Upvotes: 0

Views: 85

Answers (1)

Mateus Neves
Mateus Neves

Reputation: 764

Based on the comment given, I just changed getDocuments for addSnapshotListener

db.collection("PROFILE").document(uid).addSnapshotListener { (documentSnapshot, error) in
            guard let datas = documentSnapshot else {return}
            
            let firstname = datas.get("firstName") as! String
            
            self.firstName = firstname
        }

Upvotes: 1

Related Questions