从快照中设置全局变量

时间:2019-07-22 04:09:26

标签: swift firebase google-cloud-firestore

我正在尝试从Firestore的快照中设置currentUser变量:

var currentUser: CurrentUser!
func Login(){
  let db = Firestore.firestore()
                let userRef = db.collection("users")
                var u: CurrentUser!

                userRef.whereField("uid", isEqualTo: Auth.auth().currentUser!.uid).getDocuments { (snapshot, error) in
                    if let err = error{
                        print("Error getting documents: \(err)")
                    }else{
                        print("FOUND")
                        for document in snapshot!.documents{
                            u = CurrentUser(data: document.data())
                            print(u.username)
                          self.currentUser = u
                           print(self.currentUser.username)
                        }
                    }
                }

                self.performSegue(withIdentifier: "SEGUE_LOGGED_IN", sender: self)
            }}

self.currentUser.username始终为nil。与异步调用Im有关的不确定。如果有人可以帮助我从快照块中设置currentUser变量,那就太好了。我是Swift的新手。

0 个答案:

没有答案