0

I want to set a global variable equal to some values I am fetching from my firestore database. However, I think that because this is a

Firestore.firestore().collection("users").document(self.id).getDocument { (snapshot, err) in

                if let err = err {
                    print("Error getting documents: \(err)")
                } else{

                    self.nameGlobal = snapshot?.get("name") as! String ?? "failed"
                    self.usernameGlobal = snapshot?.get("username") as! String ?? "failed"

                }

            } 

print (self.nameGlobal) print (self.usernameGlobal)

Due to the async nature, the print statements at the end seem to be executed before the Firestore function is finished running. Does anyone know how I could force the function to finish updating the global variables before the subsequent lines are executed? I would really like to be able to access these variables outside of the callback rather than writing all my code inside of the callback.

0 Answers0