我使用parse.com
存储数据。我想用CCD_ 2&objective -c
中使用解析API的OR
条件
我的代码是:
PFQuery *query = [PFQuery queryWithClassName:@"UserInfo"];
[query whereKey:@"location" nearGeoPoint:_geoPoint withinMiles:5];
[query whereKey:@"Sex" equalTo:[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:SELECTED_LOOKINGFOR]]];
您可以给定多个约束,只有当对象与所有约束匹配时,它们才会出现在结果中。换句话说,这就像是约束的AND。例如,获得所有名为非迈克尔·亚布蒂且年龄超过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];
对于OR查询,你可以使用Parse的复合查询。例如,要获得胜利大于150或小于5的所有结果,你可以这样做:
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.
}];
现在,我希望您已经理解了这个概念和机制,您可以根据自己的要求修改查询。
您可以通过将NSPredcate与PFQuery一起使用来完成此操作。
参见以下示例
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];
有关更多详细信息,您可以访问此链接https://www.parse.com/docs/ios/guide#queries-使用nspredice链接指定约束。