Question

I am trying to search my objects on parse.com using a uisearchbar and performing 'findObjectsInBackgroundWithBlock'. I am getting the correct results in my output but they are not showing up in my table.

I was previously doing this without blocks, my code worked, it got the correct results but moved very slowly and I was getting a warning, "Warning: A long-running Parse operation is being executed on the main thread"

I had previously been using the code:

- (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];

}

So now I am trying findObjectsInBackgroundWithBlock instead, I have not worked with blocks before so this is where I need help, here is my new code:

- (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];}];

Here is some more of my code

- (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;

Any help would be greatly appreciated, cheers

Was it helpful?

Solution

In order to update the table view, you need to call the reloadData: method once you have added the new search results. Make sure that you call this method within the block that you provide to findObjectsInBackgroundWithBlock: because this block of code will be run on a separate thread. This causes the method to return instantly, and code after this method will then run before the block has actually executed. Your find objects code within filterResults: should look something like this:

[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];
    });
}];

OTHER TIPS

Differentiate from your search table view and your regular tableview when you create the cells.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
    if (tableView = self.tableView) {
        //Configure your cell normally
    }
    else {
        //Configure your cells using
        cell.someAttribute = self.searchResults[indexPath.row].someAttribute;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top