Back in objective-c, I could fetch a sectioned list of objects from Core Data using something like this:
self.fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:managedObjectContext
sectionNameKeyPath:@"ispurchased"
cacheName:nil];
And then the NSFetchedResultsController would automatically give me the data in sections and rows.
I'm experimenting with SwiftUI for the first time and I'm trying to figure out how to achieve a sectioned List like that. I've found lots of examples that use some canned array data that is pre-structured in the sectioned fashion, but I can't figure out how to do a sectioned FetchRequest nor how to integrate that with the List.
struct EasyModeList: View {
@FetchRequest(
sortDescriptors: [
NSSortDescriptor(keyPath: \EasyMode.ispurchased, ascending: true),
NSSortDescriptor(keyPath: \EasyMode.ispurchasable, ascending: false),
NSSortDescriptor(keyPath: \EasyMode.name, ascending: true),
],
animation: .default)
var easymodes: FetchedResults<EasyMode>
@Environment(\.managedObjectContext)
var viewContext
var body: some View {
List {
ForEach(self.easymodes, id: \.self) { easymode in
NavigationLink(
destination: DetailView(easymode: easymode)
) {
VStack {
Text("\(easymode.name!)")
}
}
}
}
}
}
Does SwiftUI easily support those kinds of sectioned lists? Is there a different paradigm that I should be shifting my brain to?