I have an iOS app that uses core data and CloudKit. The data model is simple and it contains a date and a string element that stores the date formatted as EEEE, MMM d yyyy
.
When I run a SectionedFetchRequest
and display the results in a list, after I have more than two sections the sort order all the sudden "jumps" and mixes the entries under each section. If I delete the app and reinstall it, the data that comes from iCloud is correctly formatted, but when I close the app and reopen it, again the sort order gets all mixed up. This happens with three or more sections and the order mix-up is not always the same (at least that I can observe).
Here's what I mean:
Two sections
Adding another entry, creating a third section
Closing the app and reopening, order gets mixed up
I have tried to look at how the data gets sorted via FetchedResults
and SectionedFetchResults
, I have tried to change how the list is created, and I have gone through the debug messages from CloudKit, but I can't figure out what's going on.
This is the data model:
And the code to fetch the data and display it on the list:
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(entity: Item.entity(), sortDescriptors: [NSSortDescriptor(keyPath: \Item.date, ascending: false)])
var entries: FetchedResults<Item>
@SectionedFetchRequest<String, Item>(
sectionIdentifier: \.dateText!,
sortDescriptors: [SortDescriptor(\.date, order: .reverse)]
)
var sections: SectionedFetchResults<String, Item>
NavigationView {
List {
ForEach(sections) { section in
Section(header: Text(section.id)) {
ForEach(section) { entry in
Text(entry.text!)
}
}
}
}
}
At this point I'm out of ideas of what else can it be. Since it's coming from iCloud formatted correctly, I'm assuming the issue is during the fetch requests and the sorting happening there, but I don't know enough to understand whether I'm doing something wrong in how I fetch the data, or whether there is another bug. Any help is appreciated.