布尔函数返回默认值,直到在Firebase DB XCode Swift中选中实值
我有以下布尔函数:布尔函数返回默认值,直到在Firebase DB XCode Swift中选中实值,swift,xcode,firebase-realtime-database,boolean-logic,boolean-expression,Swift,Xcode,Firebase Realtime Database,Boolean Logic,Boolean Expression,我有以下布尔函数: func checkIfLocateAcessGiven(uid: String) -> Bool { print("checkIfLocateAcessGiven") var currentUserID = UserDefaults.standard.string(forKey: "currentUserID") var statusStr = false
func checkIfLocateAcessGiven(uid: String) -> Bool {
print("checkIfLocateAcessGiven")
var currentUserID = UserDefaults.standard.string(forKey: "currentUserID")
var statusStr = false
print("checkIfLocateAcessGiven statusStr 1: \(statusStr)")
Database.database().reference().child("Locate").child(currentUserID!)
.observeSingleEvent(of: .value, with: { (dataSnapshot) in
if(dataSnapshot.exists()){
if(dataSnapshot.hasChild("RequestedTo")){
if(dataSnapshot.childSnapshot(forPath: "RequestedTo").hasChild(uid)){
var status = dataSnapshot.childSnapshot(forPath: "RequestedTo").childSnapshot(forPath: uid).value as! String ?? ""
if(status == "true"){
statusStr = true
print("checkIfLocateAcessGiven statusStr 2: \(statusStr)")
}else {
statusStr = false
print("checkIfLocateAcessGiven statusStr 3: \(statusStr)")
}
}else {
statusStr = false
print("checkIfLocateAcessGiven statusStr 4: \(statusStr)")
}
}else {
statusStr = false
print("checkIfLocateAcessGiven statusStr 5: \(statusStr)")
}
}else {
statusStr = false
print("checkIfLocateAcessGiven statusStr 6: \(statusStr)")
}
})
print("checkIfLocateAcessGiven statusStr 7: \(statusStr)")
return statusStr
}
调用此函数时,会得到以下结果:
checkIfLocateAcessGiven
checkIfLocateAcessGiven statusStr 1: false
checkIfLocateAcessGiven statusStr 7: false
checkIfLocateAcessGiven statusStr 2: true
由于最初返回的是false,因此我的代码采用不同的流程,然后返回true,我如何在布尔函数中等待,直到检查了实值。这两个时间间隔大约有1秒的延迟
checkIfLocateAcessGiven statusStr 7: false
及
我希望布尔值等待并检查实值,然后返回值,在本例中为true,但它当前返回false
有人能帮我一下吗?这是一个异步函数,意味着它不能同步返回异步获取所需的结果,例如从web服务(如Firebase)获得的结果 您不能(或者更确切地说,不应该)等待它,而是使用回调来通知何时获得该值 如果你不知道异步编程,网上有很多资料,你肯定应该多读一些,因为这个答案并不全面 基本思想是,异步函数接受一个在完成时调用的闭包:
func checkIfLocateAcessGiven(uid: String, completion: (Bool) -> Void) {
// your setup code
Database.database().reference().child("Locate").child(currentUserID!)
.observeSingleEvent(of: .value, with: { (dataSnapshot) in
// checks, ifs, etc..
// whenever you have the result
completion(true) // or completion(false)
}
}
调用方将指定完成回调,该回调将被异步调用:
print("before")
checkIfLocateAcessGiven(uid: "1234") { result in
print("async call result: ", result)
// do whatever you need to do with the result
}
print("after")
上面的输出如下所示:
之前
之后
异步调用结果:true
前两行将一行接一行打印,第三行将花费一段时间,无论Firebase调用需要多长时间。这是一个异步函数,意味着它无法同步返回需要异步获得的结果,例如从web服务(如Firebase)获得的结果 您不能(或者更确切地说,不应该)等待它,而是使用回调来通知何时获得该值 如果你不知道异步编程,网上有很多资料,你肯定应该多读一些,因为这个答案并不全面 基本思想是,异步函数接受一个在完成时调用的闭包:
func checkIfLocateAcessGiven(uid: String, completion: (Bool) -> Void) {
// your setup code
Database.database().reference().child("Locate").child(currentUserID!)
.observeSingleEvent(of: .value, with: { (dataSnapshot) in
// checks, ifs, etc..
// whenever you have the result
completion(true) // or completion(false)
}
}
调用方将指定完成回调,该回调将被异步调用:
print("before")
checkIfLocateAcessGiven(uid: "1234") { result in
print("async call result: ", result)
// do whatever you need to do with the result
}
print("after")
上面的输出如下所示:
之前
之后
异步调用结果:true
前两行将一行接一行地打印,第三行将需要一段时间,无论Firebase调用需要多长时间。我不知道如何异步获取值。这真的帮助了我。非常感谢。我不知道如何异步获取值。这真的帮助了我。非常感谢。