Abin George
Abin George

Reputation: 654

Parse friend request handling

I am doing a query in parse to check friend request. The query returns objects and I could log them. Now when I convert it to PFUSER, and NSLog, it gets crashed with error: 'Key "FirstName" has no data. Call fetchIfNeeded before getting its value.' Please help me out here.

PFQuery * friendQuery = [PFQuery queryWithClassName:@"friendship"];

[friendQuery whereKey:@"toUser" equalTo:[PFUser currentUser]];
[friendQuery whereKey:@"status" equalTo:@"Pending"];
[friendQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error)
    {
        //NSLog error
    }
    else {
        NSLog(@"friendRequestCount = %d", objects.count);

        PFObject *object=[objects objectAtIndex:0];
        NSLog(@"%@", object);

        self.friendRequestArray= objects;
        NSLog(@"%@",object[@"toUser"]);

        PFUser *user1= object[@"toUser"];
        NSString *friendName = [NSString stringWithFormat:@"%@ %@",user1[@"FirstName"], user1[@"LastName"] ];
        NSLog(@"name= %@",friendName);

    }
}];

Upvotes: 1

Views: 353

Answers (1)

HoanNguyen
HoanNguyen

Reputation: 405

You can use 1 or 2, I have comment the below code, not test.

PFQuery * friendQuery = [PFQuery queryWithClassName:@"friendship"];

[friendQuery whereKey:@"toUser" equalTo:[PFUser currentUser]];
[friendQuery whereKey:@"status" equalTo:@"Pending"];
//1
[friendQuery includeKey:@"toUser"];
[friendQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error)
{
    //NSLog error
}
else {
    NSLog(@"friendRequestCount = %d", objects.count);

    PFObject *object=[objects objectAtIndex:0];
    NSLog(@"%@", object);

    self.friendRequestArray= objects;
    NSLog(@"%@",object[@"toUser"]);

    PFUser *user1= object[@"toUser"];

    //2 Change PFUser to PFObject
    PFObject *user1 = object[@"toUser"];
    [user1 fetchIfNeeded];

    NSString *friendName = [NSString stringWithFormat:@"%@ %@",user1[@"FirstName"], user1[@"LastName"] ];
    NSLog(@"name= %@",friendName);

}
}];

Upvotes: 1

Related Questions