Search code examples
iospointersparse-platformpfquerytableviewcontrolle

Getting username in PFQueryTableView


I have an PFQueryTableView which I want to display all the open friends requests. I have a Parse Class named Follow which has the standard columns and a "from","to" and "requestStatus" column. "from" and "to" are pointers to PFUser. Now I query for all users who sent a friends request to the current User (in my queryForTable). After that I want the titleLabel of my cell to display the username of the ones who added me. I tried it like the following code shows, but it always gives me an error?! The first print() works and shows me PFUser: 0x7f9e6d809c30, objectId: 7SIxgfO00Z, localId: (null). The second print(requestingUser.username) always makes my app crash! How can I get the username to display it in my Table?

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

    let cell = tableView.dequeueReusableCellWithIdentifier("newFriendsCell", forIndexPath: indexPath) as! newFriendsCell

    var requestingUser = object?.objectForKey("from") as! PFUser
    print(requestingUser)
    //print(requestingUser.username)


    return cell
}

Solution

  • You need to fetch the user before accessing the username.

    requestingUser.fetchInBackgroundWithBlock({ (obj: PFObject?, error: NSError?) in
        let user = obj as! PFUser
        print(user.username)
    })