iosobjective-cipadcore-datauisearchresultscontroller

iPad SearchViewController crash _PFArray objectAtIndex beyond bounds


I'm working on the iPad version of my application and I'm facing a strange problem.

Here's the structure of the application:

UISplitViewController
->MasterViewController
-->NavigationController
--->TableViewController
---->PrototypeCells
--->SearchDisplayController
->DetailViewController
-->NavigationController
--->TableViewController
---->StaticCells

Basically, the application is displaying like 40 000 rows with CoreData and fetchResultController on the MasterView. Users can search for item with searchDisplayController or sort the TableView with a new FetchResultController. When a row is clicked, the didSelectRowAtIndexPath method set the Item in the detailView.

The problem is that sometimes when I click on the searchBar, the application crash with this error message:

**Terminating app due to uncaught exception 'NSRangeException', 
reason: ' -[_PFArray objectAtIndex:]: index (11) beyond bounds (X)'**

But I think I found a scenario that make the application crash everytimes:

  1. The application load
  2. I search for a searchString with 5 or 6 letters so the amount of items returned by the searchViewController is quite small (like 5 items). I think "beyond bounds (X)" correspond to this amount of items.
  3. I cancel the search
  4. I sort the tableView with a new FetchResultController
  5. I Type on the search bar and the application immediately crash

If I do the same scenario but with a searchString with 2 or 3 letters so the amount of items returned by the searchViewController is larger, there's no crash.

Furthermore, there's no problem/crash with the iPhone version when I apply this scenario.

I don't understand where is the problem and it's driving me crazy. Someone have an idea on what is going wrong please ? I can update this post to add code but for now, I don't know which part is usefull to understand the crash.

Thanks for reading.


Solution

  • So I think I found a solution.

    After playing with breakpoints in my TableViewController, I notice that the method which was crashing when taping on the searchBar was cellForRowAtIndexPath

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        ItemCell *cell = [self.tableView dequeueReusableCellWithIdentifier:ItemCellIdentifier forIndexPath:indexPath];
        Item *item = nil;
        if (self.searchDisplayController.active) {
            item = [self.filteredList objectAtIndex:indexPath.row];
        } else {
            item = [self.fetchedResultsController objectAtIndexPath:indexPath];
        }
        //...
        return cell;
    }
    

    Especially this line: item = [self.filteredList objectAtIndex:indexPath.row];

    I've tried to empty the filteredList when the user stop searching with this method and no more crash:

    - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
        self.filteredList=nil;
    }