Class: NSFetchedResultsController

NSObject show all


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.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from NSObject

#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version

Constructor Details

This class inherits a constructor from NSObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NSObject

Instance Attribute Details

- (String) cacheName (readonly)

The name of the file used to cache section information. The file itself is stored in a private directory; you can only access it by name using deleteCacheWithName:


- (Object) delegate

The object that is notified when the fetched results changed.


- (Array) fetchedObjects (readonly)

The results of the fetch. 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.


- (NSFetchRequest) fetchRequest (readonly)

The fetch request used to do the fetching.


- (NSManagedObjectContext) managedObjectContext (readonly)

The managed object context used to fetch objects. The controller registers to listen to change notifications on this context and properly update its result set and section information. 

- (Array) sectionIndexTitles (readonly)

The array of section index titles. 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.


- (String) sectionNameKeyPath (readonly)

The key path on the fetched objects used to determine the section they belong to.


- (Array) sections (readonly)

The sections for the receiver’s fetch results. The objects in the sections array implement the NSFetchedResultsSectionInfo protocol.You typically use the sections array when implementing UITableViewDataSource methods, such as numberOfSectionsInTableView: and tableView:titleForHeaderInSection:.


Class Method Details

+ (Object) deleteCacheWithName(name)

Deletes the cached section information with the given name.


  • name (NSString )

    The name of the cache file to delete.If name is nil, deletes all cache files.


Instance Method Details

- (NSIndexPath ) indexPathForObject(object)

Returns the index path of a given object.


  • object (Object)

    An object in the receiver’s fetch results.


  • (NSIndexPath )

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

- (Object) initWithFetchRequest(fetchRequest, managedObjectContext:context, sectionNameKeyPath:sectionNameKeyPath, cacheName:name)

Returns a fetch request controller initialized using the given arguments.


  • fetchRequest (NSFetchRequest )

    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.Important: You must not modify fetchRequest after invoking this method. For example, you must not change its predicate or the sort orderings.

  • context (NSManagedObjectContext )

    The managed object against which fetchRequest is executed.

  • sectionNameKeyPath (NSString )

    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 (NSString )

    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.


  • (Object)

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

- (Object) objectAtIndexPath(indexPath)

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


  • indexPath (NSIndexPath )

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


  • (Object)

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

- (Boolean) performFetch(error)

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


  • error (Pointer)

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


  • (Boolean)

    YES if the fetch executed successfully, otherwise NO.

- (Integer) sectionForSectionIndexTitle(title, atIndex:sectionIndex)

Returns the section number for a given section title and index in the section index. You would typically call this method when executing UITableViewDataSource’s tableView:sectionForSectionIndexTitle:atIndex: method.


  • title (NSString )

    The title of a section

  • sectionIndex (Integer)

    The index of a section.


  • (Integer)

    The section number for the given section title and index in the section index

- (NSString ) sectionIndexTitleForSectionName(sectionName)

Returns the corresponding section index entry for a given section name. 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.


  • sectionName (NSString )

    The name of a section.


  • (NSString )

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