用户 PFQuery 查询 Pointer 等关系数据的正确方法

Correct way to user PFQuery to query relational data like Pointer

好吧,假设我有一个 post 活动,用户可以单击一个按钮来通知他们正在参加此活动。截至目前,我有一个名为 Activity 的 class,我将当前用户和事件保存到此 class,因此有 2 列。如果我想查询所有参加活动的用户,我这样做的方向是正确的还是完全错误的?

到目前为止我有:

-(PFQuery*)queryForTable {

PFQuery *activityQuery = [PFQuery queryWithClassName:@"Activity"];

[activityQuery whereKey:@"event" equalTo:self.event];
[activityQuery includeKey:@"going"];

return activityQuery;
}

cellForRowAtIndex:

 UILabel *title = (UILabel*) [cell viewWithTag:1];
 title.text = [object objectForKey:@"going.username"];

到目前为止,您的代码看起来是正确的。然后要检索您的 Activity class 值,您可以使用:

PFQuery *activityQuery = [PFQuery queryWithClassName:@"Activity"];
// Set contraints here, example:
[activityQuery setLimit:100];

[query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
if (!error) {
// Success, do something with your objects.
}
}];

您实际上可以在 Parse 仪表板中看到您所做的工作。这也是他们开发这样一个数据浏览器的目的。方便多了

对于你的情况,你只需要检查类型是否为Pointer。如果在仪表板中是这样,请尝试单击它。它会将您引导至目标对象。

建议您先阅读这篇文章,它是关于关系的: https://parse.com/docs/relations_guide 然后,你应该去看看iOS SDK教程: includeKey绝对是你需要用的

这是来自 Parse 的示例:

PFQuery *query = [PFQuery queryWithClassName:@"Comment"];

// Retrieve the most recent ones
[query orderByDescending:@"createdAt"];

// Only retrieve the last ten
query.limit = 10;

// Include the post data with each comment
[query includeKey:@"post"];


[query findObjectsInBackgroundWithBlock:^(NSArray *comments, NSError *error) {
    // Comments now contains the last ten comments, and the "post" field
    // has been populated. For example:
    for (PFObject *comment in comments) {
         // This does not require a network access.
         PFObject *post = comment[@"post"];
         NSLog(@"retrieved related post: %@", post);
    }
}];