Objective-c - ¿Actualización de NSFetchedResultController?

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]

preguntado el 09 de marzo de 12 a las 16:03

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. -

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 -

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? -

Updated my question with configureCell method -

Yes, it steps into configureCell and it crashes on the first line in my method -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.