---
title: NSFetchRequest
framework: coredata
role: symbol
role_heading: Class
path: coredata/nsfetchrequest
---

# NSFetchRequest

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

## Declaration

```swift
class NSFetchRequest<ResultType> where ResultType : NSFetchRequestResult
```

## Overview

Overview An instance of NSFetchRequest collects the criteria needed to select and optionally to sort a group of NSManagedObject managed objects held in an NSPersistentStore persistent store. A fetch request contains an NSEntityDescription or an entity name that specifies which entity to search. It frequently also contains: An NSPredicate predicate that specifies which properties to filter by and the constraints on selection, such as, “last name begins with a ‘J’”. If you don’t specify a predicate, then the system fetches all instances of the entity that you specified, subject to other constraints. For more information, see fetch(_:). An array of NSSortDescriptor sort descriptors that specify how to order the returned objects, such as ascending by last name and then by first name. You can also specify other aspects of a fetch request: Use execute() to perform the fetch directly on the managed object context that’s associated with the current queue. Or use one of the NSManagedObjectContext methods such as perform(_:) to execute the fetch. note: When you execute an instance of NSFetchRequest, it always accesses the underlying persistent stores to retrieve the latest results. In SwiftUI, you can use a FetchRequest property wrapper to execute the fetch and assign the results to a property. First, create the request: let request: NSFetchRequest = {     // Create a fetch request.     let request = ShoppingItem.fetchRequest()          // Limit the maximum number of items that the request returns.     request.fetchLimit = 100                  // Filter the request results, such as to only return unchecked items.     request.predicate = NSPredicate(format: "isChecked = false")          // Sort the fetched results, such as ascending by name.     request.sortDescriptors = [NSSortDescriptor(keyPath: \ShoppingItem.name, ascending: true)]

return request }() Then use a FetchRequest property wrapper with the request to declare a property that receives the objects that the fetch returns: // Use a `FetchRequest` property wrapper to fetch the managed objects // and assign the result. @FetchRequest(fetchRequest: request) private var items: FetchedResults<ShoppingItem> tip: If you don’t need to specify multiple properties of the fetch, you can avoid creating the fetch request separately and declare it in the property wrapper instead. See FetchRequest for more information. You often predefine fetch requests in an NSManagedObjectModel managed object model to provide an API to retrieve a stored fetch request by name. Stored fetch requests can include placeholders for variable substitution, and serve as templates for later completion. Fetch request templates allow you to predefine queries with variables to substitute at runtime.

## Topics

### Managing the Fetch Request’s Entity

- [init(entityName:)](coredata/nsfetchrequest/init(entityname:)-5anoo.md)
- [init()](coredata/nsfetchrequest/init().md)
- [entityName](coredata/nsfetchrequest/entityname.md)
- [entity](coredata/nsfetchrequest/entity.md)
- [includesSubentities](coredata/nsfetchrequest/includessubentities.md)
- [NSFetchRequestResultType](coredata/nsfetchrequestresulttype.md)

### Specifying Fetch Constraints

- [predicate](coredata/nsfetchrequest/predicate.md)
- [fetchLimit](coredata/nsfetchrequest/fetchlimit.md)
- [fetchOffset](coredata/nsfetchrequest/fetchoffset.md)
- [fetchBatchSize](coredata/nsfetchrequest/fetchbatchsize.md)
- [affectedStores](coredata/nsfetchrequest/affectedstores.md)
- [NSFetchRequestExpression](coredata/nsfetchrequestexpression.md)
- [NSExpressionDescription](coredata/nsexpressiondescription.md)
- [NSFetchedPropertyDescription](coredata/nsfetchedpropertydescription.md)

### Sorting the Results

- [sortDescriptors](coredata/nsfetchrequest/sortdescriptors.md)

### Prefetching Related Objects

- [relationshipKeyPathsForPrefetching](coredata/nsfetchrequest/relationshipkeypathsforprefetching.md)

### Managing How Results Are Returned

- [resultType](coredata/nsfetchrequest/resulttype.md)
- [includesPendingChanges](coredata/nsfetchrequest/includespendingchanges.md)
- [propertiesToFetch](coredata/nsfetchrequest/propertiestofetch.md)
- [returnsDistinctResults](coredata/nsfetchrequest/returnsdistinctresults.md)
- [includesPropertyValues](coredata/nsfetchrequest/includespropertyvalues.md)
- [shouldRefreshRefetchedObjects](coredata/nsfetchrequest/shouldrefreshrefetchedobjects.md)
- [returnsObjectsAsFaults](coredata/nsfetchrequest/returnsobjectsasfaults.md)
- [NSFetchRequestResultType](coredata/nsfetchrequestresulttype.md)
- [NSFetchRequestResult](coredata/nsfetchrequestresult.md)

### Grouping and Filtering Dictionary Results

- [propertiesToGroupBy](coredata/nsfetchrequest/propertiestogroupby.md)
- [havingPredicate](coredata/nsfetchrequest/havingpredicate.md)

### Executing a Fetch Request Directly

- [execute()](coredata/nsfetchrequest/execute().md)

### Initializers

- [init(coder:)](coredata/nsfetchrequest/init(coder:).md)
- [init(entityName:)](coredata/nsfetchrequest/init(entityname:)-yd3.md)

## Relationships

### Inherits From

- [NSPersistentStoreRequest](coredata/nspersistentstorerequest.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSCopying](foundation/nscopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)

## See Also

### Fetch requests

- [NSAsynchronousFetchRequest](coredata/nsasynchronousfetchrequest.md)
- [NSAsynchronousFetchResult](coredata/nsasynchronousfetchresult.md)
- [NSFetchedResultsController](coredata/nsfetchedresultscontroller.md)
