0

I am using parse.com for storing data. I want to retrieve the objects with and & OR conditions using Parse API in objective -c

My Code is :

PFQuery *query = [PFQuery queryWithClassName:@"UserInfo"];
    [query whereKey:@"location" nearGeoPoint:_geoPoint withinMiles:5];
    [query whereKey:@"Sex" equalTo:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:SELECTED_LOOKINGFOR]]];

2 Answers 2

1

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. For Example to get all players named NOT Michael Yabuti AND aged greater than 18:

[query whereKey:@"playerName" notEqualTo:@"Michael Yabuti"];
[query whereKey:@"playerAge" greaterThan:@18];

// Using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:   @"playerName != 'Michael Yabuti' AND playerAge > 18"];
PFQuery *query = [PFQuery queryWithClassName:@"GameScore" predicate:predicate];

For OR Query, You can use Compound Queries of Parse.e.g. to get all results where wins are greater than 150 or less than 5 you do something like this Like this:

PFQuery *lotsOfWins = [PFQuery queryWithClassName:@"Player"];
[lotsOfWins whereKey:@"wins" greaterThan:[NSNumber numberWithInt:150]];

PFQuery *fewWins = [PFQuery queryWithClassName:@"Player"];
[fewWins whereKey:@"wins" lessThan:[NSNumber numberWithInt:5]];

PFQuery *query = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:fewWins,lotsOfWins,nil]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
  // results contains players with lots of wins or only a few wins.
}];

Now as I hope you have understood the concept and mechanism, you may modify your query as per your requirement.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do this by using NSPredicate with PFQuery.

See below example

NSPredicate *predicateTeamId = [NSPredicate predicateWithFormat:@"teamid = %@", objSchedule[@"teamid"]];

NSPredicate *predicateOppTeamId = [NSPredicate predicateWithFormat:@"teamid = %@", objSchedule[@"opponentteamid"]];

NSPredicate *predicateBothTeam = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicateTeamId,predicateOppTeamId]];

PFQuery * qryTeam = [PFQuery queryWithClassName:Parse_Class_Teams predicate:predicateBothTeam];

For more details , you can visit this link https://www.parse.com/docs/ios/guide#queries-specifying-constraints-with-nspredicate link.

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.