[英]Querying for 2 values in an array using Parse.com
I need to query parse.com to check if 2 specified values are present in an array. 我需要查询parse.com以检查数组中是否存在2个指定值。
The documentation states that: "You can give multiple constraints, and objects will only be in the results if they match all of the constraints. In other words, it's like an AND of constraints." 文档指出:“你可以给出多个约束,如果对象匹配所有约束,它们只会出现在结果中。换句话说,它就像一个约束的AND。”
My experience says it is not. 我的经验表明事实并非如此。
I am querying like this: 我这样查询:
NSString *user1 = [self.users objectAtIndex:0];
NSString *user2 = [self.users objectAtIndex:1];
NSLog(@"User 1: %@", user1);
NSLog(@"User 2: %@", user2);
PFQuery *gameQuery = [PFQuery queryWithClassName:@"GameObject"];
[gameQuery whereKey:@"users" equalTo:user1];
[gameQuery whereKey:@"users" equalTo:user2];
NSArray *gameObjects = [gameQuery findObjects];
NSLog(@"gameObjects: %@", gameObjects);
My log will say something like this: 我的日志会说出这样的话:
2012-04-21 14:12:23.656 Cargo[5435:707] User 1: 689XXX62
2012-04-21 14:12:23.658 Cargo[5435:707] User 2: 51XXXX994
and 和
2012-04-21 14:12:24.614 Cargo[5435:707] GameObject: <GameObject:W7qXXXPLWp> {
users = (
8XXX66,
51XXXX994
);
}
The query is clearly returning me an array of objects that match EITHER of the constraints. 查询显然返回了一个与约束条件相匹配的对象数组。 NOT both...
不是都...
How can I solve this? 我怎么解决这个问题?
Use this available method. 使用此可用方法。 This is ensure that EVERY object in specified array must be present.
这可以确保指定数组中的每个对象都必须存在。
- (void)whereKey:(NSString *)key containsAllObjectsInArray:(NSArray *)array;
PFQuery *gameQuery = [PFQuery queryWithClassName:@"GameObject"];
[gameQuery whereKey:@"users" containsAllObjectsInArray:@[user1,user2]];
NSArray *gameObjects = [gameQuery findObjects];
From Parse header documentation: 从Parse头文档:
/*!
Add a constraint to the query that requires a particular key's object to be contained in the provided array.
@param key The key to be constrained.
@param array The possible values for the key's object.
*/
- (void)whereKey:(NSString *)key containedIn:(NSArray *)array;
You will call it like this: 你会这样称呼它:
[query whereKey: @"users"
containedIn: [NSArray arrayWithObjects: user1, user2, nil]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.