1

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!

1 Answer 1

1

Your problem is that your getUser function executes an block to get the full_name value, but you are returning on another thread, so when this line return full_name executes, is almost impossible that your block has ended so your function returns "" instead of your desired value

try this instead

func getUser(userID: String,closure:((String) -> Void)?) -> Void {

        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
            closure(full_name)
        })
    }

    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, closure: { (name) in
                cell.textLabel!.text = name
            })
        }
        return cell
    }

I hope this helps you, PS I am not sure if this works because i don't have this library

Sign up to request clarification or add additional context in comments.

1 Comment

That worked! So...why did your solution work? What is a "closure"? Thank you so much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.