首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >跨关系解析PFQuery

跨关系解析PFQuery
EN

Stack Overflow用户
提问于 2014-08-12 15:15:26
回答 2查看 684关注 0票数 0

我有个定制的分析课..。

代码语言:javascript
复制
Follow
-----------------
Follower - PFUser
Followee - PFUser

我还向User表添加了一个自定义字段fullName

这是无法改变的。(虽然可以加进去)

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

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

如果PaulPeter跟在我后面,就会有跟随对象,我是跟随者,他们是跟随者。而且,Phillip没有跟踪我,所以他没有跟踪记录。

如果我使用搜索文本@"P"运行这个查询,那么它应该返回Peter和Paul,而不是Phillip。

我只是想不出如何创建查询。

我试过这样的..。

代码语言:javascript
复制
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];

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-12 20:29:28

代码语言:javascript
复制
[nameQuery whereKey:@"objectId" equalsKey:@"follower" inQuery:followQuery];

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

票数 1
EN

Stack Overflow用户

发布于 2014-08-13 09:30:23

更好的选择是将查询切换到:

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

代码语言:javascript
复制
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引用存储为字符串。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25268023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档