Skip to content

Instantly share code, notes, and snippets.

@Kentzo
Created April 25, 2010 15:50
Show Gist options
  • Select an option

  • Save Kentzo/378496 to your computer and use it in GitHub Desktop.

Select an option

Save Kentzo/378496 to your computer and use it in GitHub Desktop.
/*
Error-free way to work with NSFetchedResultsController
*/
/**
Delegate methods of NSFetchedResultsController to respond to additions, removals and so on.
*/
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.tableView beginUpdates];
insertedSections = [NSMutableArray new];
deletedSections = [NSMutableArray new];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
if (![insertedSections containsObject:[NSNumber numberWithInteger:[newIndexPath section]]]) {
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
//NSLog(@"Insert object to [%i; %i]", [newIndexPath section], [newIndexPath row]);
}
break;
case NSFetchedResultsChangeDelete:
if (![deletedSections containsObject:[NSNumber numberWithInteger:[indexPath section]]]) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//NSLog(@"Delete object from [%i; %i]", [indexPath section], [indexPath row]);
}
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
//NSLog(@"Update object at [%i; %i]", [indexPath section], [indexPath row]);
break;
case NSFetchedResultsChangeMove:
if (newIndexPath != nil) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else {
[tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationFade];
}
//NSLog(@"Move object from [%i; %i] to [%i; %i]", [indexPath section], [indexPath row], [newIndexPath section], [newIndexPath row]);
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
[insertedSections addObject:[NSNumber numberWithInteger:sectionIndex]];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
[deletedSections addObject:[NSNumber numberWithInteger:sectionIndex]];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.tableView endUpdates];
[insertedSections release];
[deletedSections release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment