Reputation: 116
I've got some code I've used elsewhere in the project and modified it to fetch user data, however there's an error saying that completion is an unresolved identifier that I'm unable to find the solution to.
I've attempted adding a completion block to the call itself, and read through Firebase documentation to try finding a solution how ever nothing seems to work.
func observeCurrentUser() {
guard let currentUser = Auth.auth().currentUser else {
return
}
CURRENT_USERS_REF?.child(currentUser.uid).observeSingleEvent(of: .value, with: {
snapshot in
if let dict = snapshot.value as? [String: Any] {
let user = User.transformUser(dict: dict)
completion(user)
}
})
}
Upvotes: 0
Views: 384
Reputation: 100549
You need to add a trailing closure ( completion ) as your current statement completion(user)
shows that you haven't added it
func observeCurrentUser(completion:@escaping((User?) -> ())) {
guard let currentUser = Auth.auth().currentUser else {
completion(nil)
return
}
CURRENT_USERS_REF?.child(currentUser.uid).observeSingleEvent(of: .value, with: {
snapshot in
if let dict = snapshot.value as? [String: Any] {
let user = User.transformUser(dict: dict)
completion(user)
}
})
}
Upvotes: 1
Reputation: 780
You have to declare the completion as a parameter on your method then specify the dataType of the completion content.
func observeCurrentUser(completion: @escaping (_ user: UserModel )->(Void)) {
guard let currentUser = Auth.auth().currentUser else {
return
}
CURRENT_USERS_REF?.child(currentUser.uid).observeSingleEvent(of: .value, with: { snapshot in
if let dict = snapshot.value as? [String: Any] {
let user = User.transformUser(dict: dict)
completion(user)
}
})
}
Upvotes: 0