在解析中搜索超过 1000 个查询 iOS

Search more than 1000 queries in parse iOS

我有一个解析 iOS 应用程序,我需要在其中搜索几千个用户 (~3,000) 的名称。我正在尝试修改我的搜索代码以便我可以执行此操作,但我需要帮助。现在我的搜索代码如下所示:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.tableView) {

        return self.objects.count;

    } else {
        return self.searchResults.count;

    }

}


-(void)filterResults:(NSString *)searchTerm :(int)limit :(int)skip {


    [self.searchResults removeAllObjects];

    PFQuery *query = [PFQuery queryWithClassName:@"_User"];
    [query whereKey:@"isTeacher" equalTo:@"False"];
    [query setLimit: limit];
    [query setSkip: skip];

    [[[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count == limit) {
            [self performTeacherQueryWithLimit:limit andSkip:skip+limit];
        }

        else{
        NSArray *results = [NSArray arrayWithArray:objects];

        NSLog(@"%@", results);
        NSLog(@"%lu", (unsigned long)results.count);
        NSLog(@"results^");


        [self.searchResults addObjectsFromArray:results];

        NSPredicate *searchPredicate =
        [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@",searchTerm];
        _searchResults = [NSMutableArray arrayWithArray:[results filteredArrayUsingPredicate:searchPredicate]];
        [self.searchDisplayController.searchResultsTableView reloadData];

        NSLog(@"%@", _searchResults);
        NSLog(@"%lu", (unsigned long)_searchResults.count);
        NSLog(@"search results^");
        }

    }];
    ]]

}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}

此代码无法运行,因为我在底部调用了 filterResults,但没有使用所有正确的参数,但那是因为我进行了一半,现在卡住了。我知道我需要使用 setSkip,但我不确定如何使它适用于我的搜索。任何帮助都是极好的!谢谢!

我使用一种同时处理查询及其结果的方法来执行此类操作,如下所示:

- (void)runQuery:(PFQuery *)query filling:(NSMutableArray *)array completion:(void (^)(BOOL))completion {
    query.skip = array.count;
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            [array addObjectsFromArray:objects];
            if (objects.count < query.limit) {
                return completion(YES);
            } else {
                [self runQuery:query filling:array completion:completion];
            }
        } else {
            return completion(NO);
        }
    }];
}

这样使用:

PFQuery *query = [PFQuery queryWithClassName:@"MyClass"];
// setup query
query.limit = // set this to a reasonable size
// the given method will do ceil(N / limit) finds, where N is the number
// of rows that satisfy the query

NSMutableArray *array = [@[] mutableCopy];
[self runQuery:query filling:array completion:^(BOOL success) {
    NSLog(@"%@", array);
    // you would do your local search and set search results here
}];

功能更强大的版本会在查询之间调用进度块。这将使您能够不断更新结果。为此,只需添加一个进度块参数并在 [array addObjectsFromArray:objects];.

之后立即调用它