我正在尝试使用uisearchbar在parse.com上搜索我的对象并执行'findObjectsInBackgroundWithBlock'。我在输出中得到了正确的结果,但是它们没有显示在我的表中。
我以前没有块地执行此操作,我的代码可以正常工作,但是得到了正确的结果,但是移动得非常缓慢,并且收到警告:“警告:正在主线程上执行长时间运行的Parse操作”
我以前一直在使用代码:
- (void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName: @"Items"];
[query whereKeyExists:@"itemName"];
[query whereKeyExists:@"itemDescription"];
[query whereKey:@"tags" containsString:searchTerm];
NSArray *results = [query findObjects];
NSLog(@"%@", results);
NSLog(@"%u", results.count);
[self.searchResults addObjectsFromArray:results];
}
所以现在我正在尝试使用findObjectsInBackgroundWithBlock,我之前没有使用过块,所以这是我需要帮助的地方,这是我的新代码:
- (void)filterResults:(NSString *)searchTerm {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName: @"Items"];
[query whereKey:@"tags" containsString:searchTerm];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(@"%@", objects);
NSLog(@"%u", objects.count);
[self.searchResults addObjectsFromArray:objects];}];
这是我的更多代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return self.objects.count;
} else {
return self.searchResults.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
NSString *uniqueIdentifier = @"cell";
HomeCell *cell = nil;
cell = (HomeCell *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
if (!cell) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"HomeCell" owner:nil options:nil];
for (id currentObject in topLevelObjects)
{
if([currentObject isKindOfClass:[HomeCell class]])
{
cell = (HomeCell *)currentObject;
break;
}
}
}
if (tableView != self.searchDisplayController.searchResultsTableView) {
NSString *itemName = [object objectForKey:@"itemName"];
NSString *itemDescription = [object objectForKey:@"itemDescription"];
//cell.textLabel.text = last;
cell.cellTitleLabel.text = itemName;
cell.descriptionLabel.text = itemDescription;
cell.priceLabel.text = [object objectForKey:@"price"];
PFFile *thumbnail = [object objectForKey:@"imageFile"];
PFImageView *thumbnailImageView = cell.imageFile;
thumbnailImageView.image = [UIImage imageNamed:@"Facebook @2x.png"];
thumbnailImageView.file = thumbnail;
[thumbnailImageView loadInBackground];
}
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]) {
PFObject *obj2 = [self.searchResults objectAtIndex:indexPath.row];
PFQuery *query = [PFQuery queryWithClassName:@"Items"];
PFObject *searchedItems = [query getObjectWithId:obj2.objectId];
NSString *itemName = [searchedItems objectForKey:@"itemName"];
NSString *itemDescription = [searchedItems objectForKey:@"itemDescription"];
cell.cellTitleLabel.text = itemName;
cell.descriptionLabel.text = itemDescription;
cell.priceLabel.text = [searchedItems objectForKey:@"itemName"];
PFFile *thumbnail = [searchedItems objectForKey:@"imageFile"];
PFImageView *thumbnailImageView = cell.imageFile;
thumbnailImageView.image = [UIImage imageNamed:@"Facebook @2x.png"];
thumbnailImageView.file = thumbnail;
[thumbnailImageView loadInBackground];
}
return cell;
任何帮助将不胜感激,
干杯
最佳答案
为了更新表视图,添加新的搜索结果后,需要调用reloadData:
方法。确保在提供给findObjectsInBackgroundWithBlock:
的块中调用此方法,因为此代码块将在单独的线程上运行。这将导致该方法立即返回,并且该方法之后的代码将在该块实际执行之前运行。 filterResults:
中的查找对象代码应如下所示:
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
// This block is called from a background thread once the query has been executed
NSLog(@"%@", objects);
NSLog(@"%u", objects.count);
[self.searchResults addObjectsFromArray:objects];
// Refresh the table view on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
[self.searchDisplayController.searchResultsTableView reloadData];
});
}];
关于ios - findObjectsInBackgroundWithBlock没有显示结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18558410/