Member-only story
How to Group Results in Core Data using SwiftUI
With iOS 15, it cannot be easier to group query results in Core Data! SectionedFetchRequest was introduced since iOS 15. It is a property wrapper type that retrieves entities, grouped into sections, from a Core Data persistent store. It greatly reduces the amount of code you would have to write, to implement a sectioned list like the user interface shown above.
So how can you do it? Use SectionedFetchRequest, instead of FetchRequest.
In the view, while the sections are being iterated, the items within a section are being iterated as well. In the example above, it gets all the history entries, and put them in sections by dates.
That’s all! It saves a lot of hassle to query and manipulate the results, doesn’t it? Enjoy!
(My latest book “macOS App Development: The SwiftUI Way” is now available. Grab your copy today!)