I have updated my app to support iOS 7 and have been faced with the problem that on one of screens in my [context save];
I get the following error:
I didn't want to mask a potential problem by setting a merge policy without understanding what was causing the NSMergeConflict
first.
In my situation I had performed a NSBatchDeleteRequest
earlier on in my code. The NSBatchDeleteRequest
is performed directly on the persistent store coordinator so the ManagedObjectContext
was unaware of the deletions and still held references to deleted objects. When I later referenced one of these objects and tried to save the context, the NSMergeConflict
was thrown.
Calling reset()
on my moc
after performing the deletion fixed the issue.
let fetchRequest = NSFetchRequest(entityName: "Tasks")
let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
try managedContext.execute(batchDeleteRequest)
managedContext.reset()