简体   繁体   English

混合两个 PFQuery 结果

[英]Mix two PFQuery results

I want to have an mix of two PFQuery results.我想要混合两个PFQuery结果。 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 ).在我下面的代码中,我得到了两个结果的交集的结果(所以所有用户名和 fullName 的用户都包含self.searchText ),我想包括所有结果(所以所有用户名的用户都等于self.searchText以及 fullName 等于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];
    }
}];

What you are trying to do is get the Union (OR) of the two sets not their intersection (AND)你要做的是得到两个集合的联合(OR)而不是它们的交集(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 https://parse.com/docs/ios_guide#queries-compound/iOS

When you say当你说

so all users who's username equals self.searchText and all users who's fullName equals self.searchText所以所有用户名等于 self.searchText 的用户和 fullName 等于 self.searchText 的所有用户

what you really mean is OR , instead of AND (because AND is what your current code does).你真正的意思是OR ,而不是AND (因为AND是你当前的代码所做的)。

To create an OR relationship you will need to create 2 different queries and then use orQueryWithSubqueries: to combine them.要创建OR关系,您需要创建 2 个不同的查询,然后使用orQueryWithSubqueries:将它们组合起来。

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

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