swift function not returning string?

winston

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!

Reinier Melian

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related