4

I need to query parse.com to check if 2 specified values are present in an array.

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."

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?

2 Answers 2

2

As detailed in PFQuery.h

Use this available method. This is ensure that EVERY object in specified array must be present.

- (void)whereKey:(NSString *)key containsAllObjectsInArray:(NSArray *)array;

Example

PFQuery *gameQuery = [PFQuery queryWithClassName:@"GameObject"];
[gameQuery whereKey:@"users" containsAllObjectsInArray:@[user1,user2]];

NSArray *gameObjects = [gameQuery findObjects];
Sign up to request clarification or add additional context in comments.

Comments

1

From Parse header documentation:

/*!
  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]];

2 Comments

Nope... This works if I need to find one of the two. I need to find both.
Sorry, I misunderstood your question. Looks like you need an "AND" version of orQueryWithSubqueries: I would shoot Parse guys an email, they are very receptive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.