Presenting data in a sectioned list with @SectionedFetchRequest
Sometimes, a planned List
View isn't enough to present a set of values. Depending on the type of data to be presented, you may need to show them in sections. Think, for example, of a settings list where the settings are aggregates for types of settings; or a list of contacts, where the contacts are aggregated into sections for the surname initial.
SwiftUI provides a mechanism for fetching objects from Core Data storage that's already been aggregated into sections, allowing it to easily be presented in a sectioned List
View.
In this recipe, we'll implement a list of contacts where the contacts are grouped into sections, depending on the initial of their surname.
Getting ready
Create a SwiftUI app called SectionedContacts
.
Before we start this recipe, complete Steps 1 to 7 of the Showing Core Data objects with @FetchRequest recipe. Then, you can complete this recipe.