iPhone OS Reference Library Apple Developer Connection spyglass button

NSFetchedResultsControllerDelegate Protocol Reference

Framework
/System/Library/Frameworks/CoreData.framework
Declared in
CoreData/NSFetchedResultsController.h
Companion guide

Overview

An instance of NSFetchedResultsController uses methods in this protocol to notify its delegate that the controller’s fetch results have been changed due to an add, remove, move, or update operations.

You should consider carefully whether you want to update the table view as each change is made. If a large number of modifications are made simultaneously—for example, if you are reading data from a background thread—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 then all pending changes have been processed) to reload the table view.

The fetched results controller reports changes to its section before changes to the fetched objects themselves.

Typical Use

You can use controllerWillChangeContent: and controllerDidChangeContent: to bracket updates to a table view whose content is provided by the fetched results controller as illustrated in the following example:

/*
 Assume self has a property 'tableView' -- as is the case for an instance of a UITableViewController
 subclass -- and a method configureCell:atIndexPath: which updates the contents of a given cell
 with information from a managed object at the given index path in the fetched results controller.
 */
 
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}
 
 
- (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];
            break;
 
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
    withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}
 
 
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
    newIndexPath:(NSIndexPath *)newIndexPath {
 
    UITableView *tableView = self.tableView;
 
    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)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

User-Driven Updates

In general, NSFetchedResultsController is designed to respond to changes at the model layer. If you allow a user to reorder table rows, then your implementation of the delegate methods must take this into account.

Typically, if you allow the user to reorder table rows, your model object has an attribute that specifies its index. When the user moves a row, you update this attribute accordingly. This, however, has the side effect of causing the controller to notice the change, and so inform its delegate of the update (using controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:). If you simply use the implementation of this method shown in “Typical Use,” then the delegate attempts to update the table view. The table view, however, is already in the appropriate state because of the user’s action.

In general, therefore, if you support user-driven updates, you should set a flag if a move is initiated by the user. In the implementation of your delegate methods, if the flag is set, you bypass main method implementations; for example:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
    newIndexPath:(NSIndexPath *)newIndexPath {
 
    if (!changeIsUserDriven) {
        UITableView *tableView = self.tableView;
        // Implementation continues...
Note: NSFetchedResultsController does not support sections being deleted as a result of a UI-driven change.

Tasks

Responding to Changes

Instance Methods

controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:

Notifies the receiver that a fetched object has been changed due to an add, remove, move, or update.

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

Parameters
controller

The fetched results controller that sent the message.

anObject

The object in controller’s fetched results that changed.

indexPath

The index path of the changed object (this value is nil for insertions).

type

The type of change. For valid values see “Results Object Change Types.”

newIndexPath

The destination path for the object for insertions or moves (this value is nil for a deletion).

Discussion

The fetched results controller reports changes to its section before changes to the fetch result objects.

Changes are reported with the following heuristics:

Special Considerations

This method may be invoked many times during an update event (for example, if you are importing data on a background thread and adding them to the context in a batch). You should consider carefully whether you want to update the table view on receipt of each message.

controller:didChangeSection:atIndex:forChangeType:

Notifies the receiver of the addition or removal of a section.

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type

Parameters
controller

The fetched results controller that sent the message.

sectionInfo

The section that changed.

sectionIndex

The index of the changed section.

type

The type of change (insert or delete). Valid values are NSFetchedResultsChangeInsert and NSFetchedResultsChangeDelete.

Discussion

The fetched results controller reports changes to its section before changes to the fetched result objects.

Special Considerations

This method may be invoked many times during an update event (for example, if you are importing data on a background thread and adding them to the context in a batch). You should consider carefully whether you want to update the table view on receipt of each message.

controllerDidChangeContent:

Notifies the receiver that the fetched results controller has been completed processing of one or more changes due to an add, remove, move, or update.

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

Parameters
controller

The fetched results controller that sent the message.

Discussion

This method is invoked after all invocations of controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: and controller:didChangeSection:atIndexPath:forChangeType: have been sent for a given change event (such as the controller receiving a NSManagedObjectContextDidSaveNotification notification).

controllerWillChangeContent:

Notifies the receiver that the fetched results controller is about to start processing of one or more changes due to an add, remove, move, or update.

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller

Parameters
controller

The fetched results controller that sent the message.

Discussion

This method is invoked before all invocations of controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: and controller:didChangeSection:atIndexPath:forChangeType: have been sent for a given change event (such as the controller receiving a NSManagedObjectContextDidSaveNotification notification).

Constants

Results Object Change Types

Specify types of change.

enum {
   NSFetchedResultsChangeInsert = 1,
   NSFetchedResultsChangeDelete = 2,
   NSFetchedResultsChangeMove = 3,
   NSFetchedResultsChangeUpdate = 4
};
Constants
NSFetchedResultsChangeInsert

Specifies that an object was inserted.

NSFetchedResultsChangeDelete

Specifies that an object was deleted.

NSFetchedResultsChangeMove

Specifies that an object was moved.

NSFetchedResultsChangeUpdate

Specifies that an object was changed.

NSFetchedResultsChangeType

A type to specify types of change.

typedef NSUInteger NSFetchedResultsChangeType;
Discussion

For valid values, see “Results Object Change Types.”



Last updated: 2009-11-17

Did this document help you? Yes It's good, but... Not helpful...