Implement delegates for Core Data's fetched re

2019-07-16 18:14发布

问题:

What advantage is there to implementing the four delegate methods:

  • (void)controllerWillChangeContent:(NSFetchedResultsController *)controller

  • (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type

  • (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath

  • (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

rather than implement:

  • (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

Any help appreciated // :)

回答1:

When you're using the FetchedResultsController with a table view, you can implement those four delegate methods to respond to each change made to your fetched results, and animate the changes as they occur in your table. However, as the NSFetchedResultsControllerDelegate documentation states:

It may be computationally expensive to animate all the changes. Rather than responding to changes individually (as illustrated in “Typical Use”), you could just implement controllerDidChangeContent: (which is sent to the delegate when all pending changes have been processed) to reload the table view

So if you're doing a bunch of changes, you can just implement controllerDidChangeContent and respond to all of them at once using something like [self.tableView reloadData].