2017-11-25 292 views
0

我目前有數據的查詢試圖找到與某個用戶名的用戶的數據庫的FireStore參考。我得到用戶後,我想嘗試登錄。但是,我注意到,查詢用戶的代碼塊,一旦被調用就會返回。有沒有辦法添加完成塊或至少停止該程序,直到完成查詢。是否有可能在firestore查詢上有完成塊?

u.name = name 
global.db.collection("users").whereField("username", isEqualTo: u.name).getDocuments(completion: { (snap, error) in 
    if error != nil { 
     print(error?.localizedDescription as Any) 
     return 
    } 
    for doc in (snap?.documents)! { 
     u.email = doc.data()["email"] as! String 
    } 
}) 

Auth.auth().signIn(withEmail: u.email, password: password, completion: { (user, error) in 
    if error != nil { 
     print(error?.localizedDescription as Any) 
     return 
    } 
    print("Succesfully Logged In") 
    self.toListSelector(user: u) 
}) 

這是鏈接到我的公司的FireStore數據庫的圖像 https://i.stack.imgur.com/hI1iY.png

+0

已經有*是*在代碼完成塊:這就是你環比'卡.documents'?正如Josh回答的那樣,您需要將'Auth.auth()。signIn'代碼**移動到**回調塊中。 –

回答

1

你需要把你的signIngetDocuments處理器,可能是for循環之後?

+0

謝謝,我現在覺得有點愚蠢,我看到了答案。 –

+0

我認爲每個人在開始使用異步代碼時都會犯這種錯誤至少一次。 –

相關問題