Overview
概要
Use a SectionedFetchResults
instance to show or edit Core Data managed objects, grouped into sections, in your app’s user interface. If you don’t need sectioning, use FetchedResults
instead.
You request a particular set of results by annotating the fetched results property declaration with a SectionedFetchRequest
property wrapper. Indicate the type of the fetched entities with a Results
type, and the type of the identifier that distinguishes the sections with a SectionIdentifier
type. 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 and grouped by a string that represents the days when earthquakes occurred:
@SectionedFetchRequest<String, Quake>(
sectionIdentifier: \.day,
sortDescriptors: [SortDescriptor(\.time, order: .reverse)]
)
private var quakes: SectionedFetchResults<String, Quake>
The quakes
property acts as a collection of SectionedFetchResults.Section
instances, each containing a collection of Quake
instances. The example above depends on the Quake
model object declaring both time
and day
properties, either stored or computed. For best performance with large data sets, use stored properties.
The collection of sections, as well as the collection of managed objects in each section, conforms to the RandomAccessCollection
protocol, so you can access them as you would any other collection. For example, you can create nested ForEach
loops inside a List
to iterate over the results:
List {
ForEach(quakes) { section in
Section(header: Text(section.id)) {
ForEach(section) { quake in
QuakeRow(quake: quake)
}
}
}
}
Don’t confuse the Section
view that you use to create a hierarchical display with the SectionedFetchResults.Section
instances that hold the fetched results.
When you need to dynamically change the request’s section identifier, predicate, or sort descriptors, set the result instance’s sectionIdentifier
, nsPredicate
, and sortDescriptors
or nsSortDescriptors
properties, respectively. Be sure that the sorting and sectioning work together to avoid discontinguous sections.
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 container that you define as part of your model:
ContentView()
.environment(
\.managedObjectContext,
QuakesProvider.shared.container.viewContext)