One Logo Pool Ball
SwiftUI · IOS API REFERENCE

SectionedFetchResults

struct · iOS 15.0+ · SDK 27.1

@MainActor @preconcurrency struct SectionedFetchResults<SectionIdentifier, Result> where SectionIdentifier : Hashable, Result : NSFetchRequestResult

A collection of results retrieved from a Core Data persistent store, grouped into sections. 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 <doc://com.apple.documentation/documentation/SwiftUI/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 ``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 <doc://com.apple.documentation/documentation/Swift/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) // Displays information about a quake. } } } } Don't confuse the ``SwiftUI/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 ``EnvironmentValues/managedObjectContext`` environment value. To support user interface activity, you typically rely on the <doc://com.apple.documentation/documentation/CoreData/NSPersistentContainer/viewContext> property of a shared <doc://com.apple.documentation/documentation/CoreData/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)

Members (0)

This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.

Skipped declarations

6 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

SectionedFetchResults.nsSortDescriptors

The containing type needs Swift generic type arguments. (no canonical witness for Result)

SectionedFetchResults.nsPredicate

The containing type needs Swift generic type arguments. (no canonical witness for Result)

SectionedFetchResults.sectionIdentifier

This declaration needs Swift generic type arguments. (no canonical witness for Result)

SectionedFetchResults.startIndex

The containing type needs Swift generic type arguments. (no canonical witness for Result)

SectionedFetchResults.endIndex

The containing type needs Swift generic type arguments. (no canonical witness for Result)

SectionedFetchResults.subscript(_:)

The containing type needs Swift generic type arguments. (no canonical witness for Result)

All SwiftUI types →