Parse - 如何将两部分查询变成单个查询?
Parse - How to turn two part query into single query?
我有 Group PFObjects 和 Item PFObjects。每个 Item 都包含一个指向它所属的 Group 的指针。一个 item 只能指向一个 Group。在我的 collection 视图中,Item objects 显示在 headers 部分下,根据它们所属的 Group到.
最初我正在构建一个查询,该查询获取属于用户的所有 Item objects,并从中构建了 Item[= 的字典34=] 数组 objects 由从每个 Item
中提取的 Group 键控
var query = PFQuery(className: "Item")
query.whereKey("createdBy", equalTo:PFUser.currentUser())
query.includeKey(“Group”)
但是,现在我遇到了问题。如果 Group 没有指向它的指针,我仍然想显示 Group。
我真正需要的是提取属于用户的所有组和指向这些组的所有项目。我知道这可以通过一对查询来完成,但是因为我是 parseUI QueryCollectionViewController 的子类,所以如果我可以在单个查询中完成它会更清晰。可能吗?
一个查询:你不能。您需要两个查询,但是:
如果您想要一个干净的解决方案,请使用 cloud code。这样一来,您就可以在 "query".
中下载任何您想要的内容
这取决于您是否知道用户是否创建了群组。 IF一个组指向创建它的用户,那么我想你可以通过一个查询来完成。我个人在 android 中进行开发,但我相信 iOS 的功能实际上与 Parse 相同。无论哪种方式:
PFQuery *query = [PFQuery queryWithClassName:@"Group"];
[query includeKey:@"Item"];
[query whereKey:@"createdBy" equalTo:PFUser.currentUser()];
[query findObjectsInBackgroundWithBlock:^(NSArray *groups, NSError *error) {
// retrieves all groups, that have been created by the current user
// A group contains all the item objects as well thanks to the include key!
}];
BUT,如果您要查找基于用户创建的项目的所有组以及所有没有项目的组,您必须使用内部查询。思路如下
Get all groups.
except the ones that have items
unless they have items of the current user.
看起来像这样:
//First inner query
PFQuery *innerQueryItems = [PFQuery queryWithClassName:@"Items"];
[query whereKey:@"createdBy" notEqualTo:@PFUser.currentUser()];
PFQuery *mainQuery = [PFQuery queryWithClassName:@"Group"];
[query whereKey:@"items" doesNotMatchesQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *groups, NSError *error) {
// retrieves all groups unless they have items that do not belong to the current user.
}];
我不确定上面的代码是否有效,但我相信这就是您的目标!
一些不错的参考资料material
Basic iOS doc from parse,总是得心应手。
Link 到您可以应用于查询的过滤器!绝对值得一试
我有 Group PFObjects 和 Item PFObjects。每个 Item 都包含一个指向它所属的 Group 的指针。一个 item 只能指向一个 Group。在我的 collection 视图中,Item objects 显示在 headers 部分下,根据它们所属的 Group到.
最初我正在构建一个查询,该查询获取属于用户的所有 Item objects,并从中构建了 Item[= 的字典34=] 数组 objects 由从每个 Item
中提取的 Group 键控var query = PFQuery(className: "Item")
query.whereKey("createdBy", equalTo:PFUser.currentUser())
query.includeKey(“Group”)
但是,现在我遇到了问题。如果 Group 没有指向它的指针,我仍然想显示 Group。
我真正需要的是提取属于用户的所有组和指向这些组的所有项目。我知道这可以通过一对查询来完成,但是因为我是 parseUI QueryCollectionViewController 的子类,所以如果我可以在单个查询中完成它会更清晰。可能吗?
一个查询:你不能。您需要两个查询,但是: 如果您想要一个干净的解决方案,请使用 cloud code。这样一来,您就可以在 "query".
中下载任何您想要的内容这取决于您是否知道用户是否创建了群组。 IF一个组指向创建它的用户,那么我想你可以通过一个查询来完成。我个人在 android 中进行开发,但我相信 iOS 的功能实际上与 Parse 相同。无论哪种方式:
PFQuery *query = [PFQuery queryWithClassName:@"Group"];
[query includeKey:@"Item"];
[query whereKey:@"createdBy" equalTo:PFUser.currentUser()];
[query findObjectsInBackgroundWithBlock:^(NSArray *groups, NSError *error) {
// retrieves all groups, that have been created by the current user
// A group contains all the item objects as well thanks to the include key!
}];
BUT,如果您要查找基于用户创建的项目的所有组以及所有没有项目的组,您必须使用内部查询。思路如下
Get all groups.
except the ones that have items
unless they have items of the current user.
看起来像这样:
//First inner query
PFQuery *innerQueryItems = [PFQuery queryWithClassName:@"Items"];
[query whereKey:@"createdBy" notEqualTo:@PFUser.currentUser()];
PFQuery *mainQuery = [PFQuery queryWithClassName:@"Group"];
[query whereKey:@"items" doesNotMatchesQuery:innerQuery];
[query findObjectsInBackgroundWithBlock:^(NSArray *groups, NSError *error) {
// retrieves all groups unless they have items that do not belong to the current user.
}];
我不确定上面的代码是否有效,但我相信这就是您的目标!
一些不错的参考资料material
Basic iOS doc from parse,总是得心应手。 Link 到您可以应用于查询的过滤器!绝对值得一试