解析好友请求处理

时间:2014-03-20 07:50:12

标签: ios objective-c parse-platform

我正在解析查询朋友请求的查询。查询返回对象,我可以记录它们。现在,当我将其转换为PFUSER和NSLog时,它会因错误而崩溃:'Key "FirstName" has no data. Call fetchIfNeeded before getting its value.'请在这里帮助我。

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

    }
}];

1 个答案:

答案 0 :(得分:1)

你可以使用1或2,我对下面的代码进行评论,而不是测试。

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

}
}];