fetch(_:)
Returns an array of items of the specified type that meet the fetch request’s critieria.
Declaration
nonisolated func fetch<T>(_ request: NSFetchRequest<T>) throws -> [T] where T : NSFetchRequestResultParameters
- request:
The fetch request that specifies the search criteria.
Mentioned in
Discussion
This method fetches objects from the context and the persistent stores that you associate with the context’s persistent store coordinator. The method registers any objects it retrieves from a store with the context.
Consider the following when fetching:
If the fetch request doesn’t have a predicate, it returns all instances of the specified entity.
The fetch results include any object in the context that’s an instance of the request’s entity, and that meets the request’s criteria, even if the context has yet to save the object to a persistent store.
The fetch request evalutes the in-memory state of each object. Therefore, the fetch results include any unsaved objects with changes that cause them to meet the request’s criteria, even if their counterparts in the persistent store don’t. Conversely, the results don’t include unsaved objects with in-memory changes that mean they no longer meet the criteria, even if their store versions do.
The fetch results don’t include deleted objects, even if the context has yet to save the deletion to the persistent store.
A fetch never changes realized objects, or those with pending changes, without developer intervention. If you fetch objects, modify them, and then execute a new fetch that includes a superset of those objects, you don’t receive new instances of those objects. Instead, you receive the existing objects with their current in-memory state.