Question

I want to have an mix of two PFQuery results. In my code below, I'm getting the results of the intersection of the two results (so all users who's username AND fullName contain self.searchText), I want to to include all the results (so all users who's username equals self.searchText and all users who's fullName equals self.searchText).

PFQuery *query = [PFUser query];
[query whereKey:@"username" containsString:self.searchText];
[query whereKey:@"fullName" containsString:self.searchText];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"%@ %@", error, [error userInfo]);
    } else {
        self.searchResults = [[NSMutableArray alloc] initWithArray:objects];
        NSLog(@"%@", objects);
        [self.tableView reloadData];
    }
}];
Was it helpful?

Solution

What you are trying to do is get the Union (OR) of the two sets not their intersection (AND)

use the following code should bring you all users that match either of those criterias

PFQuery *firstQuery = [PFUser query];
[firstQuery whereKey:@"username" containsString:self.searchText];

PFQuery *secondQuery = [PFUser query];
[secondQuery whereKey:@"fullName" containsString:self.searchText];

PFQuery *query = [PFQuery orQueryWithSubqueries:@[firstQuery ,secondQuery ]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
  if (error) {
    NSLog(@"%@ %@", error, [error userInfo]);
} else {
    self.searchResults = [[NSMutableArray alloc] initWithArray:objects];
    NSLog(@"%@", objects);
    [self.tableView reloadData];
}
}];

and here is the link to the guide

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

OTHER TIPS

When you say

so all users who's username equals self.searchText and all users who's fullName equals self.searchText

what you really mean is OR, instead of AND (because AND is what your current code does).

To create an OR relationship you will need to create 2 different queries and then use orQueryWithSubqueries: to combine them.

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