2

So I am using an NSFetchedResultController to display data in my table. I am getting an update notification, and both index and newIndex are set. So what do these two indexes represent during an update? and Why Am i getting values for both on an update notification? Isn't newIndex for move notification only?

When i try to acces object at index [24, 4] I get a crash, because apparently an object doesn't exist at this indexPath. any idea what's going on here?

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath 
     forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    switch(type) 
    {
    case NSFetchedResultsChangeInsert:
        [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeUpdate:
        [self configureCell:[_tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
        break;

    case NSFetchedResultsChangeMove:
        [_tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
                          withRowAnimation:UITableViewRowAnimationFade];
        [_tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] 
                          withRowAnimation:UITableViewRowAnimationFade];
        break;
    }
}


- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo 
           atIndex:(NSUInteger)sectionIndex 
     forChangeType:(NSFetchedResultsChangeType)type 
{
    switch(type) 
    {
        case NSFetchedResultsChangeInsert:
            [_tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            [_tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
  }
}

- (void)configureCell:(MyCustomCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
        // Crashes on the line below, before calling setBook
    Book *book = [_fetchedResultController objectAtIndexPath:indexPath];
    [cell setBook:book];
}

Result during an update, right before my crash:

indexPath     [24, 4]
newIndexePath [32, 4]
aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • Which case is being executed? (Insert, Delete, Move, Update) Is it the one you would expect to be executes? You said you are getting an update notification, but I just want to be clear if it's really an update, per se. What does the result come form? It's not something printed by this code. – Jim Mar 09 '12 at 16:53
  • During the crash Update is being executed, using newIndexPath instead of indexPath would prevent the crash, but I need to know why, and which one is the correct index to be used during an update – aryaxt Mar 09 '12 at 16:55
  • newIndexPath is only for insertions or moves, so that looks right. Can you show your configureCell:atIndexPath: method? Can you step into configureCell before the crash? – Jim Mar 09 '12 at 17:00
  • Updated my question with configureCell method – aryaxt Mar 09 '12 at 17:04
  • Yes, it steps into configureCell and it crashes on the first line in my method – aryaxt Mar 13 '12 at 04:26
  • I ended up using newIndexPath for my updates, that get's rid of the crash, but I'm not sure whether that's the correct solution or not – aryaxt Mar 17 '12 at 17:19
  • I had the same problem, but I did not see this thread, therefore I posted my own question and the solution that I found myself 5 days later [here](http://stackoverflow.com/questions/11432556/nsrangeexception-exception-in-nsfetchedresultschangeupdate-event-of-nsfetchedres). My solution is not to use the indexPath (or newIndexPath) at all for update events, but to use the anObject parameter of the -controller:didChangedObject:... delegate method. – Martin R Jul 16 '12 at 11:41

0 Answers0