Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PFQuery with multiple constraints on one key

i have parse.com database. i have column in a table named "text". i have to find text with multiple keys matching with AND condition. i tried it all ways. i tried to use:

    PFQuery *query=[PFQuery queryWithClassName:@"post"];
    [query whereKey:@"text" containedIn:Array];

or

    PFQuery *query=[PFQuery queryWithClassName:@"post"];
    [query whereKey:@"text" containsAllObjectsInArray:Array];        

or

PFQuery *query=[PFQuery queryWithClassName:@"post"];
for (NSString *str in filtersArray) {

    [query whereKey:@"text" containsString:str];

}

but no one works. please guide me if parse sdks supports this or not totally? if yes how can i achieve the results. many thanks in advance :)

EIDT:

for example i have three entries in database as text:

  1. "i have a data entry"

  2. "i can not find something in data"

  3. "how can i do this"

if pass "i" and "this" it should return entry (3)

if pass "i" and "data" it should return entry (1,2)

if pass "i" and "else" it should return nothing

like image 461
Imran Ahmed Avatar asked Jul 12 '15 21:07

Imran Ahmed


1 Answers

The reason your query doesn't work is because Parse does not support having the same constraint (in this case 'containsString:') more than once on the same key.

So, what I would suggest is to query for a regular expression which will match all of your filter strings using - (instancetype)whereKey:(NSString *)key matchesRegex:(NSString *)regex.

NSArray *qryStrings = @[str1, str2, str3, ....]; //strings you are trying to match

//string which will hold our regular expression
NSString *regexString = @"^"; //start off the regex

for (NSString *str in qryStrings) { //build the regex one filter at a time
    regexString = [NSString stringWithFormat:@"%@(?=.*%@)", regexString, str];
}

regexString = [NSString stringWithFormat:@"%@.*$", regexString]; //finish off the regex

PFQuery *query = [PFQuery queryWithClassName:@"post"];
[query whereKey:@"text" matchesRegex:regexString];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        //objects array contains matching rows with matching strings
    }
    else {
        NSLog(@"%@ %@", error, [error userInfo]);
    }
}];
like image 190
keither04 Avatar answered Oct 09 '22 06:10

keither04