An instance of
NSFetchRequest describes search criteria used to retrieve data from a Persistent store.
- iOS 3.0+
- macOS 10.4+
- tvOS 3.0+
- watchOS 2.0+
An instance collects the criteria needed to select and—optionally—order a group of Managed object, or data about records held in a persistent store. A fetch request must contain an entity description (an instance of
NSEntityDescription) that specifies which entity to search. It frequently also contains:
A predicate (an instance of
NSPredicate) that specifies which properties to select by and the constraints on selection, for example “last name begins with a ‘J’”. If you don’t specify a predicate, then all instances of the specified entity are selected (subject to other constraints, see
fetch(_:)for full details).
An array of sort descriptors (instances of
NSSortDescriptor) that specify how the returned objects should be ordered, for example by last name then by first name.
You can also specify other aspects of a fetch request:
The maximum number of objects that a request should return; see
Which data stores the request should access; see
Whether the fetch returns managed objects or just object IDs; see
Which properties to fetch; see
The offset for the fetch to start; see
Whether unsaved changes should be included; see
You can also fetch distinct property values, and attribute values that satisfy a given function, as illustrated in Core Data Snippets.
You often predefine fetch requests in a managed object model—
NSManagedObjectModel provides API to retrieve a stored fetch request by name. Stored fetch requests can include placeholders for variable substitution, and so serve as templates for later completion. Fetch request templates therefore allow you to pre-define queries with variables that are substituted at runtime.