iOS Developer Library

Developer

CoreData Framework Reference NSFetchedResultsController Class Reference

Options
Deployment Target:

On This Page
Language:

NSFetchedResultsController

You use a fetched results controller to efficiently manage the results returned from a Core Data fetch request to provide data for a UITableView object. More...

Inheritance


Conforms To


Import Statement


import CoreData @import CoreData;

Availability


Available in iOS 3.0 and later.
  • Returns a fetch request controller initialized using the given arguments.

    Declaration

    Swift

    init(fetchRequest fetchRequest: NSFetchRequest, managedObjectContext context: NSManagedObjectContext, sectionNameKeyPath sectionNameKeyPath: String?, cacheName name: String?)

    Objective-C

    - (id)initWithFetchRequest:(NSFetchRequest *)fetchRequest managedObjectContext:(NSManagedObjectContext *)context sectionNameKeyPath:(NSString *)sectionNameKeyPath cacheName:(NSString *)name

    Parameters

    fetchRequest

    The fetch request used to get the objects.

    The fetch request must have at least one sort descriptor. If the controller generates sections, the first sort descriptor in the array is used to group the objects into sections; its key must either be the same as sectionNameKeyPath or the relative ordering using its key must match that using sectionNameKeyPath.

    context

    The managed object against which fetchRequest is executed.

    sectionNameKeyPath

    A key path on result objects that returns the section name. Pass nil to indicate that the controller should generate a single section.

    The section name is used to pre-compute the section information.

    If this key path is not the same as that specified by the first sort descriptor in fetchRequest, they must generate the same relative orderings. For example, the first sort descriptor in fetchRequest might specify the key for a persistent property; sectionNameKeyPath might specify a key for a transient property derived from the persistent property.

    name

    The name of the cache file the receiver should use. Pass nil to prevent caching.

    Pre-computed section info is cached to a private directory under this name. If Core Data finds a cache stored with this name, it is checked to see if it matches the fetchRequest. If it does, the cache is loaded directly—this avoids the overhead of computing the section and index information. If the cached information doesn’t match the request, the cache is deleted and recomputed when the fetch happens.

    Return Value

    The receiver initialized with the specified fetch request, context, key path, and cache name.

    Import Statement

    import CoreData

    Availability

    Available in iOS 3.0 and later.

  • Executes the receiver’s fetch request.

    Declaration

    Swift

    func performFetch(_ error: NSErrorPointer) -> Bool

    Objective-C

    - (BOOL)performFetch:(NSError **)error

    Parameters

    error

    If the fetch is not successful, upon return contains an error object that describes the problem.

    Return Value

    YEStrue if the fetch executed successfully, otherwise NOfalse.

    Discussion

    After executing this method, you can access the receiver’s the fetched objects with the property fetchedObjects.

    Special Considerations

    This method returns NOfalse (and a suitable error in error) if the fetch request doesn’t include a sort descriptor that uses the section name key path specified in initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:.

    Import Statement

    import CoreData

    Availability

    Available in iOS 3.0 and later.

  • The results of the fetch.

    Declaration

    Swift

    var fetchedObjects: [AnyObject]? { get }

    Objective-C

    @property(nonatomic, readonly) NSArray *fetchedObjects

    Discussion

    The value of the property is nil if performFetch: hasn’t been called.

    The results array only includes instances of the entity specified by the fetch request (fetchRequest) and that match its predicate. (If the fetch request has no predicate, then the results array includes all instances of the entity specified by the fetch request.)

    The results array reflects the in-memory state of managed objects in the controller’s managed object context, not their state in the persistent store. The returned array does not, however, update as managed objects are inserted, modified, or deleted.

    Import Statement

    import CoreData

    Availability

    Available in iOS 3.0 and later.

  • Returns the object at the given index path in the fetch results.

    Declaration

    Swift

    func objectAtIndexPath(_ indexPath: NSIndexPath) -> AnyObject

    Objective-C

    - (id)objectAtIndexPath:(NSIndexPath *)indexPath

    Parameters

    indexPath

    An index path in the fetch results.

    If indexPath does not describe a valid index path in the fetch results, an exception is raised.

    Return Value

    The object at a given index path in the fetch results.

    Import Statement

    import CoreData

    Availability

    Available in iOS 3.0 and later.

  • Returns the index path of a given object.

    Declaration

    Swift

    func indexPathForObject(_ object: AnyObject) -> NSIndexPath?

    Objective-C

    - (NSIndexPath *)indexPathForObject:(id)object

    Parameters

    object

    An object in the receiver’s fetch results.

    Return Value

    The index path of object in the receiver’s fetch results, or nil if object could not be found.

    Special Considerations

    In versions of iOS before 3.2, this method raises an exception if object is not contained in the receiver’s fetch results.

    Import Statement

    import CoreData

    Availability

    Available in iOS 3.0 and later.

  • Returns the corresponding section index entry for a given section name.

    Declaration

    Swift

    func sectionIndexTitleForSectionName(_ sectionName: String?) -> String?

    Objective-C

    - (NSString *)sectionIndexTitleForSectionName:(NSString *)sectionName

    Parameters

    sectionName

    The name of a section.

    Return Value

    The section index entry corresponding to the section with name sectionName.

    Discussion

    The default implementation returns the capitalized first letter of the section name.

    You should override this method if you need a different way to convert from a section name to its name in the section index.

    Special Considerations

    You only need this method if you use a section index.

    Import Statement

    import CoreData

    Availability

    Available in iOS 3.0 and later.

  • The array of section index titles.

    Declaration

    Swift

    var sectionIndexTitles: [AnyObject] { get }

    Objective-C

    @property(nonatomic, readonly) NSArray *sectionIndexTitles

    Discussion

    The default implementation returns the array created by calling sectionIndexTitleForSectionName: on all the known sections. You should override this method if you want to return a different array for the section index.

    Special Considerations

    You only need this method if you use a section index.

    Import Statement

    import CoreData

    Availability

    Available in iOS 3.0 and later.