Reputation: 31
My app is crashing whenever I try to save the Core Data MOC with a "Could Not Merge Changes" error. The baffling part to me is that it only occurs with a SQLite file that I seed to the app upon initial launch when the SQLite file has 40k+ records on one of the tables. The data displays perfectly fine in tableViews and I can update existing objects. I just can't save any new objects without crashing.
I've taken the same SQLite file (to maintain the same schema), removed all records, added back in a small quantity of records on the tables and everything works fine in the app -- I can move between view controllers, add new objects to the Core Data entities, and save the MOC anywhere.
I'm using a Core Data stack in a singleton. I've logged each request for the MOC and there is only one on the main thread. The app is very basic with no background tasks running. My build target is iOS 13.0.
The error is also confusing because the error isn't even on the new object that I'm inserting into Core Data. It is for some other existing object in Core Data and is different each time it crashes (For instance this would occur when I add a BetaCat or DeltaCat object):
Fatal error: Unresolved error Error Domain=NSCocoaErrorDomain Code=133020 "Could not merge changes." UserInfo={conflictList=(
"NSMergeConflict (0x600000025480) for NSManagedObject (0x600003661900) with objectID '0xe3afe1928ab74f71 <x-coredata://E62171B1-E38F-4BF5-B012-DB1842171304/TextCategory/p4>' with oldVersion = 0 and newVersion = <deleted> and old cached row = {\n category = AlphaCat;\n isLocked = \"<null>\";\n}"
), NSExceptionOmitCallstacks=true}, ["conflictList": <__NSArrayM 0x6000007e35d0>(
NSMergeConflict (0x600000025480) for NSManagedObject (0x600003661900) with objectID '0xe3afe1928ab74f71 <x-coredata://E62171B1-E38F-4BF5-B012-DB1842171304/TextCategory/p4>' with oldVersion = 0 and newVersion = <deleted> and old cached row = {
category = AlphaCat;
isLocked = "<null>";
I've read everything that I can find on the merge change error and it almost always seems to be an issue related to multiple MOCs trying to save data and there being conflicts which doesn't seem to apply to my situation.
Does anyone have any idea why there are issues with the larger SQLite file and not the smaller one?
EDIT Code example of how I get a MOC, set entity attributes, and save MOC:
let newCategory = Category(context: CoreDataStack.getContext())
newCategory.attribute1 = textField.text!
newCategory.attribute2 = true
CoreDataStack.saveContext()
getContext() simply returns a persistentContainer.viewContext
and saveContext() is a basic:
if context.hasChanges {
do {
try context.save()
} catch let error as NSError {
let nserror = error as NSError
fatalError("Unresolved error when saving MOC: \(nserror), \(nserror.userInfo)")
}
}
Upvotes: 1
Views: 1114
Reputation: 11
If your context is shared, it is best to set its mergePolicy in advance, like this:
yourContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy//OR: NSMergeByPropertyObjectTrumpMergePolicy
Upvotes: 0