Overview
概要
Use a FetchRequest
property wrapper to declare a FetchedResults
property that provides a collection of Core Data managed objects to a SwiftUI view. The request infers the entity type from the Result
placeholder type that you specify. Condition the request with an optional predicate and sort descriptors. For example, you can create a request to list all Quake
managed objects that the Loading and Displaying a Large Data Feed sample code project defines to store earthquake data, sorted by their time
property:
@FetchRequest(sortDescriptors: [SortDescriptor(\.time, order: .reverse)])
private var quakes: FetchedResults<Quake>
Alternatively, when you need more flexibility, you can initialize the request with a configured NSFetchRequest
instance:
@FetchRequest(fetchRequest: request)
private var quakes: FetchedResults<Quake>
Always declare properties that have a fetch request wrapper as private. This lets the compiler help you avoid accidentally setting the property from the memberwise initializer of the enclosing view.
The fetch request and its results use the managed object context stored in the environment, which you can access using the managedObjectContext
environment value. To support user interface activity, you typically rely on the viewContext
property of a shared NSPersistentContainer
instance. For example, you can set a context on your top level content view using a shared container that you define as part of your model:
ContentView()
.environment(
\.managedObjectContext,
QuakesProvider.shared.container.viewContext)
When you need to dynamically change the predicate or sort descriptors, access the request’s FetchRequest.Configuration
structure. To create a request that groups the fetched results according to a characteristic that they share, use SectionedFetchRequest
instead.