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