Presenting data in a sectioned list with @SectionedFetchRequest
Sometimes, a plain 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 it in sections. Think, for example, of a settings list where the settings are aggregated into types of settings, or a list of contacts, where the contacts are aggregated into sections by the first characters of surnames.
SwiftUI provides a mechanism for fetching objects from Core Data storage that’s already been aggregated into sections, allowing it to be easily presented in a sectioned List
view.
In this recipe, we’ll implement a list of contacts, where the contacts are grouped into sections determined by the first character of their surname.
Getting ready
Create a SwiftUI app called SectionedContacts
.
Before we start this recipe, make sure to complete steps 1 to 10 of the Showing Core Data objects with @FetchRequest recipe. Then, you can complete...