I came across a code snippet that semi-explains how to move items in a list bound to CoreData. What I don’t understand is how it is working since map would return a copy and not a reference to the original array. Aside from saving, it’s never updating the fetched results data.
func move(from source: IndexSet, to destination: Int) {
// Make an array of groups from fetched results
var revisedGroups: [PrayerGroup] = self.groups.map{$0}
// Change the order of the groups in the array
revisedGroups.move(fromOffsets: source, toOffset: destination)
// Update the order attribute in revisedGroups to persist the
// the new order
for reverseIndex in stride(from: revisedGroups.count - 1, through: 0, by: -1) {
revisedGroups[reverseIndex].order = Int16(reverseIndex)
}
try? moc.save()
}