在关系中解析PFQuery

时间:2014-08-12 15:15:26

标签: ios objective-c parse-platform pfquery

我有一个自定义的Parse类......

Follow
-----------------
Follower - PFUser
Followee - PFUser

我还在User表中添加了自定义字段fullName

这不能改变。 (虽然可以添加)

我已经使用它完成了几个查询,我在最后一个上遇到了绊脚石。

我希望能够运行一个查询,该查询返回fullName包含某些给定文本的用户,但只包含跟随当前用户的用户。

如果PaulPeter跟随我,那么我将成为跟随对象,并且他们是关注者。此外,Phillip没有关注我,因此没有关注他的记录。

如果我使用搜索文本@"P"运行此查询,那么它应该返回彼得和保罗,而不是菲利普。

我无法弄清楚如何创建查询。

我尝试过这样的事情......

PFQuery *followQuery = [CCFollow query];
[followQuery whereKey:@"followee" equalTo:[PFUser currentUser]];

PFQuery *nameQuery = [PFUser query];
[nameQuery whereKey:@"fullName" contains:searchText];
[nameQuery whereKey:@"objectId" equalsKey:@"follower" inQuery:followQuery];

但它不会返回错误,也不会返回任何对象。

2 个答案:

答案 0 :(得分:1)

[nameQuery whereKey:@"objectId" equalsKey:@"follower" inQuery:followQuery];

这行代码不正确。这里,关键objectId的类型为string,而关注者的类型为PFUser。您可以创建一个额外的列" followerString"它以字符串格式存储关注者的objectId,以便您进行比较。

答案 1 :(得分:1)

更好的选择是切换查询:

获取所有Follow条记录,其中followee是当前用户,follower包含在与User查询匹配的fullName条记录中。< / p>

PFQuery *followQuery = [CCFollow query];
[followQuery whereKey:@"followee" equalTo:[PFUser currentUser]];

PFQuery *nameQuery = [PFUser query];
[nameQuery whereKey:@"fullName" contains:searchText];

// here's the difference:
[followQuery whereKey:@"follower" matchesQuery:nameQuery];
// include follower
[followQuery includeKey:@"follower"];
// now run find on followQuery (not nameQuery)
[followQuery findObjectsInBackgroundWithBlock:^(NSArray *follows, NSError *error) {
    // "follows" now contains the follow records, and the "follower" field
    // has been populated. For example:
    for (PFObject *follow in follows) {
         // This does not require a network access.
         PFObject *follower = follow[@"follower"];
         NSLog(@"retrieved related user: %@", follower);
         // could put them in an array or whatever to bind to the UI
    }
}];

这样做您无需更改架构或开始将objectId引用存储为字符串。