beginner_T
beginner_T

Reputation: 427

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
}

Upvotes: 0

Views: 22

Answers (2)

Marius Waldal
Marius Waldal

Reputation: 9942

The correct way to solve this is to add

query.includeKey("from")

to you initial query.

Your

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

will then have a value.

Upvotes: 0

Dominic K
Dominic K

Reputation: 7075

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)
})

Upvotes: 0

Related Questions