Structure

SectionedFetchRequest

A property wrapper type that retrieves entities, grouped into sections, from a Core Data persistent store.

Declaration 宣言

@propertyWrapper struct SectionedFetchRequest<SectionIdentifier, Result> where SectionIdentifier : Hashable, Result : NSFetchRequestResult

Overview 概要

Use a SectionedFetchRequest property wrapper to declare a SectionedFetchResults property that provides a grouped collection of Core Data managed objects to a SwiftUI view. If you don’t need sectioning, use FetchRequest instead.

Configure a sectioned fetch request with an optional predicate and sort descriptors, and include a sectionIdentifier parameter to indicate how to group the fetched results. Be sure that you choose sorting and sectioning that work together to avoid discontiguous sections. For example, you can request a list of earthquakes, composed of Quake managed objects that the Loading and Displaying a Large Data Feed sample code project defines to store earthquake data, sorted by time and grouped by date:


@SectionedFetchRequest<String, Quake>(
    sectionIdentifier: \.day,
    sortDescriptors: [SortDescriptor(\.time, order: .reverse)]
)
private var quakes: SectionedFetchResults<String, Quake>

Always declare properties that have a sectioned 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 request infers the entity type from the Result type that you specify, which is Quake in the example above. Indicate a SectionIdentifier type to declare the type found at the fetched object’s sectionIdentifier key path. The section identifier type must conform to the Hashable protocol.

The example above depends on the Quake type having a day property that’s either a stored or computed string. Be sure to mark any computed property with the @objc attribute for it to function as a section identifier. For best performance with large data sets, use stored properties.

The sectioned 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 section identifier, predicate, or sort descriptors, access the request’s SectionedFetchRequest.Configuration structure, either directly or with a binding.

Topics 話題

Creating a Fetch Request フェッチリクエストを作成する

Creating a Fully Configured Fetch Request

Configuring a Request Dynamically

Getting the Fetched Results 取って来た結果を取得する

Default Implementations 省略時実装

Relationships 関係

Conforms To 次に準拠

  • DynamicProperty
    Conforms when SectionIdentifier conforms to Hashable and Result conforms to NSFetchRequestResult.

See Also 参照

Core Data