Reputation: 570
I am attempting to create a completion block while pulling a users profile from a firebase table. I need it to complete before I allow it to pass back a value.
Here is what I have so far:
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
}
}
}
I'm getting the following error:
Cannot convert value of type '() -> _' to expected argument type '((Error) -> Void)?'
I'm not sure how to fix this, any suggestion would be appreciated.
Upvotes: 0
Views: 97
Reputation: 907
I'm not sure if that'll fix the error. If it doesn't, then I think I know the issue and it would be with your error block and your observeEvent.
Edit: Just made a change to return an error object from the 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.
}
}
}
Upvotes: 1