结合条件查询是不是正常工作?

时间:2014-05-05 16:03:57

标签: ios objective-c parse-platform

我需要使用parse.com方法orQueryWithSubquerries:来创建组合条件查询。

这是我的代码:

PFQuery *testQuery = [PFUser query];
[testQuery whereKey:@"displayName notEqualTo:@"Bob"];

PFQuery *testQuery2 = [PFUser query];
[testQuery whereKey:@"username" notEqualTo:@"frank"];

PFQuery *orQuery = [PFQuery orQueryWithSubqueries:@[testQuery, testQuery2]];
[orQuery findObjectsInBackgroundWithBlock:^(NSArray *users, NSError *error) {
    if (!error) {
        PFUser *user = [users firstObject];
    }
}];

代码运行正常,但它返回我的所有用户对象,包括应从2个查询中过滤掉的对象。我使用这种方法是错误的,还是有某种方式我需要使用它?

编辑:

我还尝试使用此方法like how this parse.com example does,但它仍无法正常使用。

PFQuery *testQuery = [PFUser query];
[testQuery whereKey:@"numberOfPhotos" greaterThan:@(3)];

PFQuery *testQuery2 = [PFUser query];
[testQuery whereKey:@"numberOfPhotos" lessThan:@(1)];

PFQuery *orQuery = [PFQuery orQueryWithSubqueries:@[testQuery, testQuery2]];
[orQuery findObjectsInBackgroundWithBlock:^(NSArray *users, NSError *error) {
    if (!error) {
        // users still contains every user in the app even though it shouldn't according to the parse example
    }
}];

3 个答案:

答案 0 :(得分:2)

您不需要进行复合查询,只需定期查询:

PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"displayName" notEqualTo:@"Bob"];
[userQuery whereKey:@"username" notEqualTo:@"frank"];
[userQuery findObjectsInBackgroundWithBlock:^(NSArray *users, NSError *error) {
    if (!error) {
        PFUser *user = [users firstObject];
    }
}];

你现在混淆了复合查询的使用,

TestQuery =显示不是bob的所有用户。 (包括用户名为frank的用户)

TestQuery2 =用户名不是坦诚的所有用户。 (包括显示名称为Bob的用户)

当你将它们组合起来时,你会得到所有用户,你的化合物是矛盾的。

基本上,query1中遗漏的所有用户都包含在query2中。而且,query2中遗漏的所有用户都包含在query1中。当您组合这些查询时,它们会填充缺少的空间,并且您将获得所有用户。

更新

如果你正在尝试使用hasPrefix,它似乎应该可以正常工作:

NSString * prefixToSearch = ...;

PFQuery * displayNameQuery = [PFUser query];
[displayNameQuery whereKey:@"displayName" hasPrefix:prefixToSearch];

PFQuery * usernameQuery = [PFUser query];
[usernameQuery whereKey:@"username" hasPrefix:prefixToSearch];

PFQuery * compoundQuery = [PFQuery orQueryWithSubqueries:@[displayNameQuery, usernameQuery]];
[orQuery findObjectsInBackgroundWithBlock:^(NSArray *users, NSError *error) {
    if (!error) {
        NSLog(@"Found %i users", users.count);
    }
}];

更新2 - 让我们测试

PFQuery *testQuery = [PFUser query];
[testQuery whereKey:@"numberOfPhotos" greaterThan:@(3)];

PFQuery *testQuery2 = [PFUser query];
[testQuery whereKey:@"numberOfPhotos" lessThan:@(1)];

PFQuery *orQuery = [PFQuery orQueryWithSubqueries:@[testQuery, testQuery2]];
[orQuery findObjectsInBackgroundWithBlock:^(NSArray *users, NSError *error) {
    if (!error) {
        // users still contains every user in the app even though it shouldn't according to the parse example

        for (PFUser * user in users) {
            int numberOfPhotos = [user[@"numberOfPhotos"] intValue];
            if (1 <= numberOfPhotos && numberOfPhotos <= 3) {
                NSLog(@"Query is failing");
            }
        }

    }
}];

更新3 - 谓词!

通过我们的谈话,我们得到了它:

NSPredicate * predicate = [NSPredicate predicateWithFormat:@"keyOne BEGINSWITH 'z' OR keyTwo BEGINSWITH 'a'"]; 
PFQuery * userQuery = [PFQuery queryWithClassName:@"_User" predicate:predicate]; 
[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    NSLog(@"Found objects: %@", objects); 
}];

答案 1 :(得分:0)

在再次调查之后,我最终将我的解决方案改为基于正则表达式而不是谓词。出于我的目的,这意味着我不需要创建额外的小写显示名称属性,因为Parse正则表达式允许不区分大小写的选项。此外,我可以更灵活地更改搜索参数。

我的代码现在看起来像这样:

// Uses regex to remove white space at the beginning and end of search text
NSString *modifiedSearchText = [Utility stringTrimmedForLeadingAndTrailingWhiteSpacesFromString:self.searchText];

PFQuery *userUsernameQuery = [PFUser query];
// the @"i" modifier is stated in the parse documentation and means case-insensitive
[userUsernameQuery whereKey:@"username" matchesRegex:modifiedSearchText modifiers:@"i"];

PFQuery *userDisplaynameQuery = [PFUser query];
[userDisplaynameQuery whereKey:@"displayName" matchesRegex:modifiedSearchText modifiers:@"i"];

PFQuery *userQuery = [PFQuery orQueryWithSubqueries:@[userUsernameQuery, userDisplaynameQuery]];
[userQuery orderByAscending:@"username"];

return userQuery;

答案 2 :(得分:0)

在iOS 8.3中,xcode 6.3.1

{{1}}

搜索大写或小写的单词