A description of search criteria used to retrieve data from a persistent store.
- iOS 3.0+
- macOS 10.4+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
- Core Data
An instance of
NSFetch collects the criteria needed to select and optionally to sort a group of managed objects held in a persistent store. (See
NSPersistent and NSManagedObject.) A fetch request must contain an entity description (an instance of
NSEntity) or an entity name that specifies which entity to search. It frequently also contains:
A predicate (an instance of
NSPredicate) that specifies which properties to filter 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
executefor full details).
Fetch Request: error:
An array of sort descriptors (instances of
NSSort) 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
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—
NSManaged provides an 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 predefine queries with variables that are substituted at runtime.