访问 Firebase 中的用户帐户时生成的完成块错误
Completion Block Error Generated when accessing User account in Firebase
我试图在从 firebase table 中提取用户配置文件时创建一个完成块。在允许它传回一个值之前,我需要它完成。
这是我目前的情况:
func getProf(email: String, pass: String, completionBlock: @escaping (_ success: Bool) -> (Int)) {
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
self.zDaily = value?["qday"] as? Int ?? 0
}) {
if let error = error {
completionBlock(false)
} else {
completionBlock(true)
return zDaily
}
}
}
我收到以下错误:
Cannot convert value of type '() -> _' to expected argument type '((Error) -> Void)?'
我不确定如何解决这个问题,如有任何建议,我们将不胜感激。
我不确定这是否会修复错误。如果没有,那么我想我知道问题所在,那就是你的错误块和你的 observeEvent。
编辑:刚刚更改了 return 来自 observeEvent 的错误对象。
func getProf(email: String, pass: String, completionBlock: @escaping (Bool, Int) -> ()) {
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
self.zDaily = value?["qday"] as? Int ?? 0
}) { (error) in //Added return error value - this may fix your error
if let error = error {
completionBlock(false, 0) //Add default 0 to return if error
} else {
completionBlock(true, zDaily) //Added zDaily as Int to return
//return zDaily - return will not return in async function so you can return the value in completionBlock above.
}
}
}
我试图在从 firebase table 中提取用户配置文件时创建一个完成块。在允许它传回一个值之前,我需要它完成。
这是我目前的情况:
func getProf(email: String, pass: String, completionBlock: @escaping (_ success: Bool) -> (Int)) {
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
self.zDaily = value?["qday"] as? Int ?? 0
}) {
if let error = error {
completionBlock(false)
} else {
completionBlock(true)
return zDaily
}
}
}
我收到以下错误:
Cannot convert value of type '() -> _' to expected argument type '((Error) -> Void)?'
我不确定如何解决这个问题,如有任何建议,我们将不胜感激。
我不确定这是否会修复错误。如果没有,那么我想我知道问题所在,那就是你的错误块和你的 observeEvent。
编辑:刚刚更改了 return 来自 observeEvent 的错误对象。
func getProf(email: String, pass: String, completionBlock: @escaping (Bool, Int) -> ()) {
let ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("users").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
self.zDaily = value?["qday"] as? Int ?? 0
}) { (error) in //Added return error value - this may fix your error
if let error = error {
completionBlock(false, 0) //Add default 0 to return if error
} else {
completionBlock(true, zDaily) //Added zDaily as Int to return
//return zDaily - return will not return in async function so you can return the value in completionBlock above.
}
}
}