0
我做了一個連接到firebase的函數,轉到特定路徑,然後給我值。當我打印(snapshot.value)時,它給了我我需要的價值。當我調用該函數時,userProfile爲空。我需要userProfile來返回快照String。返回聲明在Swift中使用Firebase時出錯
func getUserData(uid: String) -> String{
var _REF_USERNAME = FIRDatabase.database().reference().child("users").child(uid).child("profile").child("username")
var userProfile = String()
_REF_USERNAME.observe(.value, with: {(snapshot) in
print("SNAP: \(snapshot.value)")
userProfile = snapshot.value as! String
})
print(userProfile)
return userProfile
}
從Firebase返回數據之前執行返回。 Firebase數據在閉包內成爲* only *有效。請參閱我的答案[這個問題](http://stackoverflow.com/questions/43823808/access-firebase-variable-outside-closure/43832208#43832208),也[這一個](http://stackoverflow.com /問題/ 43130730 /表視圖-IS-未顯示最評論或 - 用戶名 - 從-火力/ 43157576#43157576)。您通常不應嘗試從Firebase函數返回數據,因爲它是異步的,其餘代碼是同步的。 – Jay