Generic Class

NSFetchRequest

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

Overview

An instance of NSFetchRequest collects the criteria needed to select and optionally to sort a group of managed objects 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 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 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.

Topics

Managing the Fetch Request’s Entity

init()

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.

struct NSFetchRequestResultType

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

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.

class NSFetchRequestExpression

An expression that evaluates the result of a fetch request on a managed object context.

class NSExpressionDescription

A special property description type intended for use with a fetch request.

class NSFetchedPropertyDescription

A description object used to define which properties are fetched from Core Data.

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.

struct NSFetchRequestResultType

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

protocol NSFetchRequestResult

An abstract protocol used with parameterized fetch requests.

Grouping and Filtering Dictionary Results

var propertiesToGroupBy: [Any]?

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

var havingPredicate: NSPredicate?

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

Executing a Fetch Request Directly

func execute()

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

Relationships

Generic Constraints

  • ResultType : NSFetchRequestResult
    

Conforms To

See Also

Fetch Requests

class NSAsynchronousFetchRequest

A fetch request that retrieves results asynchronously and supports progress notification.

class NSAsynchronousFetchResult

A fetch result object that encompasses the response from an executed asynchronous fetch request.

class NSFetchedResultsController

A controller that you use to manage the results of a Core Data fetch request and display data to the user.