3

I need to do a query on an object in parse to see either of 2 fields contain a value. The value is a PFUser object. I presume I need some kind of OR statement but I don't know how to do that.

Here's the code I have so far..

  PFQuery *query = [PFQuery queryWithClassName:@"GameTradeObject"];

    [query whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];

    [query whereKey:@"fromUser" equalTo:[PFUser currentUser]];
    [query whereKey:@"toUser" equalTo:[PFUser currentUser]];

    [query includeKey:@"fromUser"];
    [query includeKey:@"fromVillage"];

    [query includeKey:@"toUser"];
    [query includeKey:@"toVillage"];

    [query findObjectsInBackgroundWithBlock:^(NSArray *PUObjects, NSError *error) {
        if (!error) {

What I need is to know if the current user Object exists in either of the fromUser or toUser fields. I think it might have to be done with an NSPredicate but I don't know how to build the statement.

3 Answers 3

3

You can use [PFQuery orQueryWithSubqueries:@[query1, query2]] to combine two queries.

  PFQuery *query1 = [PFQuery queryWithClassName:@"GameTradeObject"];
  [query1 whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
  [query1 whereKey:@"fromUser" equalTo:[PFUser currentUser]];

  PFQuery *query2 = [PFQuery queryWithClassName:@"GameTradeObject"];
  [query2 whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
  [query2 whereKey:@"toUser" equalTo:[PFUser currentUser]];

  PFQuery *query = [PFQuery orQueryWithSubqueries:@[query1, query2]];
  [query includeKey:@"fromUser"];
  [query includeKey:@"fromVillage"];
  [query includeKey:@"toUser"];
  [query includeKey:@"toVillage"];
  [query findObjectsInBackgroundWithBlock:...
Sign up to request clarification or add additional context in comments.

Comments

0

Another option, which will typically be faster to execute, is to store both users in a single array. You might consider:

PFQuery *query = [PFQuery queryWithClassname:@"GameTradeObject"];
// array equalTo string returns all objects where the array contains the string
[query whereKey:@"users" equalTo:PFUser.currentUser];
[query includeKey:@"users"];
[query includeKey:@"villages"];
[query findObjectsInBackgroundWithBlock:...];

Comments

0

You can simply implement below way;

  PFQuery *query1 = [PFQuery queryWithClassName:@"GameTradeObject"];
  [query1 whereKey:@"fromUser" equalTo:[PFUser currentUser]];
  [query1 whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
  PFQuery *query2 = [PFQuery queryWithClassName:@"GameTradeObject"];
  [query2 whereKey:@"toUser" equalTo:[PFUser currentUser]];
  [query2 whereKey:@"region" equalTo:[NSNumber numberWithInt:region]];
  PFQuery *query = [PFQuery orQueryWithSubqueries:@[query1, query2]];

And you won't need to use second time includeKey because you filtered on query1 and query2 as fromUser and toUser columns.

Comments

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.