I created a function to return a username with a Firebase query on the userID parameter. I want to use this username to populate the text labels in a tableView. Although the query within the function returns the correct value, the value does not seem to be returned:
func getUser(userID: String) -> String {
var full_name: String = ""
rootRef.child("users").child(userID).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user value
let first_name = snapshot.value!["first_name"] as! String
let last_name = snapshot.value!["last_name"] as! String
full_name = first_name + " " + last_name
print(full_name) // returns correct value
})
return full_name //printing outside here just prints a blank space in console
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let inviteDict = invites[indexPath.row].value as! [String : AnyObject]
if let userID = inviteDict["invitedBy"] as? String {
let name = getUser(userID)
cell.textLabel!.text = name
}
return cell
}
}
The cells don't have text. Printing the function return to console just prints blank space. Any ideas as to what's wrong?
Thanks!