Generic Class

NSFetchRequest

An instance of NSFetchRequest describes search criteria used to retrieve data from a Persistent store.

Overview

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:

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 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.

Symbols

Managing the Fetch Request’s Entity

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 receiver.

var includesSubentities: Bool

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

Fetch Constraints

var predicate: NSPredicate?

The predicate of the receiver.

var fetchLimit: Int

The fetch limit of the receiver.

var fetchOffset: Int

The fetch offset of the receiver.

var fetchBatchSize: Int

The batch size of the receiver.

var affectedStores: [NSPersistentStore]?

The persistent stores specified for the receiver.

Sorting

var sortDescriptors: [NSSortDescriptor]?

The sort descriptors of the receiver.

Prefetching

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 receiver.

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 using the receiver 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.

Constants

NSFetchRequestResultType

These constants specify the possible result types a fetch request can return.

Initializers

Instance Methods

Relationships

Generic Constraints

  • ResultType : NSFetchRequestResult

Conforms To