简体   繁体   English

组合条件查询无法正常工作?

[英]Combining conditional queries isn't working properly?

I'm needing to use the parse.com method orQueryWithSubquerries: to create a combined conditional query. 我需要使用parse.com方法orQueryWithSubquerries:创建组合的条件查询。

Here is my code for it: 这是我的代码:

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];
    }
}];

The code runs fine, but it returns all of my user objects, including the ones that should be filtered out from the 2 queries. 该代码运行良好,但是它返回了我的所有用户对象,包括应该从2个查询中过滤掉的对象。 Am I using this method wrong, or is there a certain way that I need to use it? 我使用此方法是否错误,或者我需要使用某种方法吗?

Edit: 编辑:

I have also attempted to use this method exactly like how this parse.com example does , but it still doesn't work properly. 我也曾尝试完全按照此parse.com示例的方式使用此方法,但仍然无法正常工作。

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
    }
}];

You don't need to do a compound query, just query regularly: 您无需执行复合查询,只需定期查询:

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];
    }
}];

You're confusing the use of a compound query, as you have it currently, 您目前无法使用复合查询,

TestQuery = All users whose display isn't bob. TestQuery =所有用户的显示不是bob。 (includes users whose username is frank) (包括用户名坦率的用户)

TestQuery2 = All users whose username isn't frank. TestQuery2 =用户名不坦率的所有用户。 (includes users whose display name is Bob) (包括显示名称为Bob的用户)

When you combine them, you get all of the users, your compound is contradictory. 当您将它们组合在一起时,您会得到所有用户,您的化合物是矛盾的。

Basically, all of the users left out in query1 are included in query2. 基本上,query1中遗漏的所有用户都包含在query2中。 And, all the users left out in query2 are included in query1. 并且,query2中遗漏的所有用户都包含在query1中。 When you combine these queries, they fill in the missing space and you get all users. 当您组合这些查询时,它们会填补缺少的空间,您会得到所有用户。

Update 更新

If you're trying to do hasPrefix, it seems like it should work fine: 如果您尝试执行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);
    }
}];

Update 2 - Let's Test 更新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");
            }
        }

    }
}];

Update 3 - Predicates! 更新3-谓词!

Via our conversation, we got it working with this: 通过我们的对话,我们可以使用它:

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); 
}];

After looking into this once again I ended up changed my solution to being regex based rather than predicate. 再次研究之后,我最终将解决方案更改为基于正则表达式而不是谓词。 For my purposes that means I don't need to create an additional lowercase display name property since Parse regex allows for case-insensitive options. 就我而言,这意味着我不需要创建其他小写显示名称属性,因为Parse regex允许使用不区分大小写的选项。 Also I can more easily change the parameters of the search with higher levels of flexibility. 同样,我可以更轻松地以更高的灵活性来更改搜索参数。

My code now looks like this: 我的代码现在看起来像这样:

// 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;

In iOS 8.3, xcode 6.3.1 在iOS 8.3中,xcode 6.3.1

query.whereKey("name", matchesRegex:searchBar.text, modifiers:"i")

searches upper or lowercase words 搜索大写或小写单词

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM