Question

I am using parse and and am trying to use the below code to try check which users have a certain string in the array @"Id1". However i'm not getting any objects back. Is whereKey:containsString only for searching parse string values? If so how can I search the parse arrays to see if they contain a string? Thanks in advance.

NSString *searchId = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchId"];

PFQuery *query = [PFUser query]; //1

 [query whereKey:@"Id1" containsString:searchId];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {//4
    if (!error) {
        NSLog(@"1");
        self.ObjectsArray = nil;
        self.ObjectsArray = [[NSArray alloc] initWithArray:objects];
    } else {
        [[[UIAlertView alloc] initWithTitle:@"Error" message:@"Please make sure you have a proper device connection." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
    }
}];
Was it helpful?

Solution

If you are looking for the full string it is much easier. To look for a single value in an array, just use whereKey:equalTo: as described here:

https://parse.com/docs/ios_guide#queries-arrays/iOS

[query whereKey:@"Id1" equalTo:searchId];

This returns rows where at least one entry in the array is equal to the search criteria (handles numbers, strings, boolean, etc).

If instead you truly want to find records where at least one item in the array contains a string within it, you will have to get tricky.

One option would be to have a non-visible field called (e.g.) "Id1_search". Create a before-update Cloud Code handler that joins all array values into a comma list, converting to lowercase too. Then you can use whereKey:containsString: on that new field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top