<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/SectionedFetchResults",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI21SectionedFetchResultsV"
  },
  "title" : "SectionedFetchResults"
}
-->

# SectionedFetchResults

A collection of results retrieved from a Core Data persistent store,
grouped into sections.

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

## 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`](/documentation/SwiftUI/FetchedResults) instead.

You request a particular set of results by annotating the fetched results
property declaration with a [`SectionedFetchRequest`](/documentation/SwiftUI/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 [`SectionedFetchResults.Section`](/documentation/SwiftUI/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
<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`](/documentation/SwiftUI/ForEach) loops inside a [`List`](/documentation/SwiftUI/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 [`Section`](/documentation/SwiftUI/Section) view that you use to create a
hierarchical display with the [`SectionedFetchResults.Section`](/documentation/SwiftUI/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`](/documentation/SwiftUI/SectionedFetchResults/sectionIdentifier), [`nsPredicate`](/documentation/SwiftUI/SectionedFetchResults/nsPredicate), and [`sortDescriptors`](/documentation/SwiftUI/SectionedFetchResults/sortDescriptors) or
[`nsSortDescriptors`](/documentation/SwiftUI/SectionedFetchResults/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`](/documentation/SwiftUI/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)
```

## Topics

### Configuring the associated sectioned fetch request

[`nsPredicate`](/documentation/SwiftUI/SectionedFetchResults/nsPredicate)

The request’s predicate.

[`sortDescriptors`](/documentation/SwiftUI/SectionedFetchResults/sortDescriptors)

The request’s sort descriptors, accessed as value types.

[`nsSortDescriptors`](/documentation/SwiftUI/SectionedFetchResults/nsSortDescriptors)

The request’s sort descriptors, accessed as reference types.

[`sectionIdentifier`](/documentation/SwiftUI/SectionedFetchResults/sectionIdentifier)

The key path that the system uses to group fetched results into sections.

[`Section`](/documentation/SwiftUI/SectionedFetchResults/Section)

A collection of fetched results that share a specified identifier.

### Getting indices

[`startIndex`](/documentation/SwiftUI/SectionedFetchResults/startIndex)

The index of the first section in the results collection.

[`endIndex`](/documentation/SwiftUI/SectionedFetchResults/endIndex)

The index that’s one greater than that of the last section.

### Getting results

[`subscript(_:)`](/documentation/SwiftUI/SectionedFetchResults/subscript(_:))

Gets the section at the specified index.

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`Collection`](/documentation/Swift/Collection)

[`Sequence`](/documentation/Swift/Sequence)

[`RandomAccessCollection`](/documentation/Swift/RandomAccessCollection)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`BidirectionalCollection`](/documentation/Swift/BidirectionalCollection)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)