一些用户未出现在搜索解析中 iOS
Some users not showing up in search parse iOS
我有一个 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" 和 "isTeacher" 是真的,但我很困惑。附件是parse core
中示例用户的截图
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
的数量上限 PFQuery
可以 return 虽然是 1000,因此由于您有超过 1000 个用户并且假设需要接收超过 1000 个执行不同查询时的结果,您可以通过在使用递增的 skip
参数的同时遍历多个查询来指定 "number of objects to skip before returning any."
因此,尽管我编写的第一段代码 return 来自该查询的前 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
.
我有一个 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" 和 "isTeacher" 是真的,但我很困惑。附件是parse core
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
的数量上限 PFQuery
可以 return 虽然是 1000,因此由于您有超过 1000 个用户并且假设需要接收超过 1000 个执行不同查询时的结果,您可以通过在使用递增的 skip
参数的同时遍历多个查询来指定 "number of objects to skip before returning any."
因此,尽管我编写的第一段代码 return 来自该查询的前 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
.