我有一个iOS解析应用程序,有时需要搜索所有用户。但是出于某种原因,用户存在于数据库中,但其他用户无法在搜索中看到它们。我看到用户之间没有相关性或者理由是我唯一想到的可能是解析不是搜索所有用户?这是搜索的代码
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.parseClassName = @"_User";
self.textKey = @"name";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = NO;
}
return self;
}
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"isTeacher" equalTo:@"True"];
[query whereKey:@"schoolName" equalTo:[[PFUser currentUser] objectForKey:@"schoolName"]];
return query;
}
我假设如果上面有问题,但如果需要,其余的代码就在这里:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"Cell";
PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = [object objectForKey:@"name"];
cell.detailTextLabel.text = [object objectForKey:@"username"];
}
// Configure the cell
if (tableView == self.tableView) {
cell.textLabel.text = [object objectForKey:@"name"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
PFObject* object = self.searchResults[indexPath.row];
//UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
cell.textLabel.text = [object objectForKey:@"name"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *teacherUsername = cell.textLabel.text
;
//NSLog(teacherUsername);
[[NSUserDefaults standardUserDefaults] setObject:teacherUsername forKey:@"teacherUsername"];
[self performSegueWithIdentifier:@"next" sender:self];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return self.objects.count;
} else {
return self.searchResults.count;
}
}
-(void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"isTeacher" equalTo:@"True"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
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;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
为什么某些用户不会出现?我检查了显而易见的事情,确保用户拥有相同的" schoolName"和#34;是老师"是的,但我很难过。附件是解析核心
中的示例用户的屏幕截图答案 0 :(得分:1)
Parse
查询的默认限制是100个对象,因此即使您期望170 PFObjects
,也需要指定您希望从查询中接收170多个对象以便全部接收它们来自使用limit
参数的查询,例如:
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"isTeacher" equalTo:@"True"];
[query setLimit: 1000]; // <-- increase the limit up to 1000
PFObject
sa PFQuery
的数量上限虽然可以返回1000,但由于您拥有超过1000个用户,并且在执行其他查询时可能需要获得超过1000个结果,您可以通过循环使用增加的skip
参数的多个查询来指定&#34;在返回任何内容之前跳过的对象数。&#34;
因此,虽然我编写的第一个代码块将返回该查询中的前1000个对象,但可以检索下一个1000对象:
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"isTeacher" equalTo:@"True"];
[query setLimit: 1000]; // <-- increase the limit up to 1000
[query setSkip: 1000]; // <-- skip the first 1000 already found
一般来说,虽然最好一点一点地收到你的结果并增加setSkip以便只在你绝对需要它时才能收到更多的结果,你可以假设一次检索所有与你的查询匹配的对象,比如这样:
- (void)theOriginalCallingMethod {
// Start out by fetching the maximum number of results
// from the query and start at the beginning, i.e.
// not skipping anything
[self performTeacherQueryWithLimit:1000 andSkip:0];
}
- (void)performTeacherQueryWithLimit:(int)limit andSkip:(int)skip {
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"isTeacher" equalTo:@"True"];
[query setLimit: limit];
[query setSkip: skip];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// If the maximum number of objects is found, there
// may be more, so continue querying
if (objects.count == limit) {
// Perform the query using the same limit, but increase
// the skip amount by that current limit to indicate
// that the next query should skip the results we just
// found
[self performTeacherQueryWithLimit:limit andSkip:skip+limit];
}
// ...other code...
}];
}
注意:只有PFTableView
属性设置为paginationEnabled
时,此功能才能用于NO
。