Generic Class


A description of search criteria used to retrieve data from a persistent store.


An instance of NSFetchRequest collects the criteria needed to select and—optionally—order a group of managed objects, or data about records held in a persistent store. (See NSPersistentStore and NSManagedObject.) A fetch request must contain an entity description (an instance of NSEntityDescription) 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 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:

You can also fetch distinct property values, and attribute values that satisfy a given function, as illustrated in Core Data Snippets.

You use NSFetchRequest objects with the methods fetch(_:) and count(for:), defined by NSManagedObjectContext.

You often predefine fetch requests in a managed object model—NSManagedObjectModel 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.


Managing the Fetch Request’s Entity


Creates a new fetch request.

init(entityName: String)

Initializes a fetch request configured with a given entity name.

var entityName: String?

The name of the entity the request is configured to fetch.

var entity: NSEntityDescription?

The entity specified for the fetch request.

var includesSubentities: Bool

A Boolean value that indicates whether the fetch request includes subentities in the results.

Specifying Fetch Constraints

var predicate: NSPredicate?

The predicate of the fetch request.

var fetchLimit: Int

The fetch limit of the fetch request.

var fetchOffset: Int

The fetch offset of the fetch request.

var fetchBatchSize: Int

The batch size of the objects specified in the fetch request.

var affectedStores: [NSPersistentStore]?

An array of persistent stores specified for the fetch request.

Sorting the Results

var sortDescriptors: [NSSortDescriptor]?

The sort descriptors of the fetch request.

Prefetching Related Objects

var relationshipKeyPathsForPrefetching: [String]?

The relationship key paths to prefetch along with the entity for the request.

Managing How Results Are Returned

var resultType: NSFetchRequestResultType

The result type of the fetch request.

var includesPendingChanges: Bool

A Boolean value that indicates whether, when the fetch is executed, it matches against currently unsaved changes in the managed object context.

var propertiesToFetch: [Any]?

A collection of either property descriptions or string property names that specify which properties should be returned by the fetch.

var returnsDistinctResults: Bool

A Boolean value that indicates whether the fetch request returns only distinct values for the fields specified by propertiesToFetch.

var includesPropertyValues: Bool

A Boolean value that indicates whether, when the fetch is executed, property data is obtained from the persistent store.

var shouldRefreshRefetchedObjects: Bool

A Boolean value that indicates whether the property values of fetched objects will be updated with the current values in the persistent store.

var returnsObjectsAsFaults: Bool

A Boolean value that indicates whether the objects resulting from a fetch request are faults.

Grouping and Filtering Dictionary Results

var propertiesToGroupBy: [Any]?

An array of objects that indicate how data should be grouped before a select statement is run in an SQL database.

var havingPredicate: NSPredicate?

The predicate used to filter rows being returned by a query containing a GROUP BY directive.



Constants that specify the possible result types a fetch request can return.

Executing a Fetch Request Directly

func execute()

Executes the fetch request against the managed object context that is associated with the current queue.


Generic Constraints

  • ResultType : NSFetchRequestResult

Conforms To