SectionedFetchRequest in SwiftData

With Core Data and SwiftUI we can use @SectionedFetchRequest. Does SwiftData support something similar to @SectionedFetchRequest?

For example, I want to create a lazy-loaded list that groups posts by their date.

@Model Post {
    let title: String
    let dateString: String // YYYY-MM-DD
    let createdAt: Date
}

@SectionedFetchRequest(
    entity: \Post.self,
    sectionIdentifier: \Post.dateString,
    sortDescriptors: [\Post.createdAt]
)
var postsByDate: SectionedFetchResults

ForEach(postsByDate) { section in
    Section(header: Text(section.id)) {
        ForEach(section) { post in
            PostView(post)
        }
    }
}

SwiftData doesn't provide a counterpart for SectionedFetchRequest of today. Folks typically implement the feature with your own effort if they need it. That is a common use case though, and so I’d suggest that you file a feedback report to request the feature – If you do so, please share your report ID here for folks to track.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

SectionedFetchRequest in SwiftData
 
 
Q