Search code examples
iosobjective-cparse-platformpfqueryobjective-c-2.0

PFQuery includeKey doesn't seem to be working: Key data unavailible


I am building a social app and am currently building a PFQueryTable View Controller that I would like to display a list of followers for the user selected in a previous screen.

under queryForTable i have the below query set:

-(void) queryForTable {

PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    [query whereKey:@"type" equalTo:@"follow"];
    [query whereKey:@"fromUser" equalTo:self.user];
    [query whereKey:@"toUser" notEqualTo:self.user];
    [query whereKeyExists:@"toUser"];
    [query includeKey:@"toUser"];
    [query orderByDescending:@"createdAt"];

    [query setCachePolicy:kPFCachePolicyNetworkOnly];

    return query;

}

When I perform a NSLog for object under cellForRowAtIndexPath:

- (FollowViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    static NSString *FollowCellIdentifier = @"FriendCell";

    FollowViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FollowCellIdentifier];
    if (cell == nil) {
        cell = [[FollowViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FollowCellIdentifier];
    }

    NSLog(@"OBJECT IS %@",object);


    cell.userLabel.text = [[object objectForKey:@"toUser"] objectForKey:@"username"];



        return cell;
}

the result is

OBJECT IS { ACL = ""; fromUser = ""; toUser = ""; type = follow; }

If I try to then create:

cell.userLabel.text = [[object objectForKey:@"toUser"] objectForKey:@"username"];

The console gives me an error of:

'Key "username" has no data. Call fetchIfNeeded before getting its value.'

If I step the process with breaks,the userLabel.text populates fine. but if I run it without the breaks I always get the error. Is there something I am missing here?

Any help would be great as I have been trying to fix this for the past 4 days.


Solution

  • Try:

    PFUser *user = (PFUser *)[object objectForKey:@"toUser"];
    [user fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
      cell.userLabel.text = [object objectForKey:@"username"];
    }];