Merge Conflicts without reset()
theslash opened this issue · 2 comments
Sorry for perhaps asking stupid questions, but I'm pulling my hair here and can't get
it to work properly.
I have a simple Application with a tableView that is filled with coreData.
Just one entity called "Person", no relationships, 4 attributes: name, age, recordData, recordID.
I have pulled the CoreData functions in a separate class called persistenceService.
In my AppDelegate I enabled CloudCore like this:
CloudCore.enable(persistentContainer: persistenceService.persistentContainer)
In my viewcontroller I have two functions I use for this:
#1: Fetch CoreData and reload Tableview
func getCoreData() {
// get data from coredata
let fetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
let sortDescriptor = NSSortDescriptor(key: "recordID", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
do {
self.people = try persistenceService.context.fetch(fetchRequest)
} catch {}
self.tableView.reloadData()
}
#2 Fetch Cloud Data
func fetchCloudData() {
print("fetchAndSave and Reloading Tableview")
CloudCore.fetchAndSave(to: persistenceService.persistentContainer, error: { (error) in
print("FetchAndSave error: \(error)")
}) {
DispatchQueue.main.async {
persistenceService.context.reset()
self.getCoreData()
}
}
}
What works:
I create a new entry, it gets synced to iCloud.
I delete a record in iCloud, call fetchCloudData() and its gone.
I edit a record, call fetchCloudData() and its edited in local storage.
BUT all this only works when I call persistenceService.context.reset() in the fetchCloudData()
function.
Lets say I start the app, CloudCore syncs, and then I try to edit/delete an entry I always get
an error like this:
["conflictList": <__NSArrayM 0x600000648df0>(
NSMergeConflict (0x60000086fd80) for NSManagedObject (0x604000294a00) with objectID '0xd000000000580000 <x-coredata://BB8BBB3E-0F70-4ED2-BDCD-D15AB86480B0/Person/p22>' with oldVersion = 7 and newVersion = 8 and old object snapshot
When I start the app and once call the fetchCloudData() function, it works, when I
uncomment the persistenceService.context.reset() part it gets the same error as above.
can someone please hint me what I'm doing wrong?
omg, sorry...