Core Data crash while trying to merge

I'm looking for guidance how to mitigate this crash. It seems super deep inside Core Data' FRC fetchedObjects management.

In my code, it's initiated by this

viewContext.perform {
	[unowned self] in
	self.viewContext.mergeChanges(fromContextDidSave: notification)
}

which is directly followed by the stack trace below.

Basically merging data from .NSManagedObjectContextDidSave notification from another NSManagedObjectContext. Nothing special, it works great for years, apart from these rare occurrences.

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Reason: -[__NSCFArray objectAtIndex:]: index (235) beyond bounds (234)
Termination Reason: ****** 6 Abort trap: 6

Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                	0x199e947cc __exceptionPreprocess + 164 (NSException.m:249)
1   libobjc.A.dylib               	0x1971672e4 objc_exception_throw + 88 (objc-exception.mm:356)
2   CoreFoundation                	0x199fc4258 _NSArrayRaiseBoundException + 368 (NSCFArray.m:22)
3   CoreFoundation                	0x199e288a4 -[__NSCFArray objectAtIndex:] + 200 (NSCFArray.m:42)
4   CoreData                      	0x1a1e17338 -[_PFMutableProxyArray objectAtIndex:] + 40 (_PFArray.m:1860)
5   CoreData                      	0x1a1e1673c -[NSFetchedResultsController _updateFetchedObjectsWithInsertChange:] + 380 (NSFetchedResultsController.m:1582)
6   CoreData                      	0x1a1e1426c __82-[NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:]_block_invoke + 2240 (NSFetchedResultsController.m:2171)
7   CoreData                      	0x1a1dcdf80 developerSubmittedBlockToNSManagedObjectContextPerform + 156 (NSManagedObjectContext.m:4002)
8   CoreData                      	0x1a1e41a44 -[NSManagedObjectContext performBlockAndWait:] + 216 (NSManagedObjectContext.m:4113)
9   CoreData                      	0x1a1e41034 -[NSFetchedResultsController _core_managedObjectContextDidChange:] + 124 (NSFetchedResultsController.m:2379)
10  CoreFoundation                	0x199e632f4 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 148 (CFNotificationCenter.c:701)
11  CoreFoundation                	0x199e63210 ___CFXRegistrationPost_block_invoke + 88 (CFNotificationCenter.c:194)
12  CoreFoundation                	0x199e63158 _CFXRegistrationPost + 436 (CFNotificationCenter.c:222)
13  CoreFoundation                	0x199e6170c _CFXNotificationPost + 728 (CFNotificationCenter.c:1248)
14  Foundation                    	0x198a84ea4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 92 (NSNotification.m:531)
15  CoreData                      	0x1a1e11650 -[NSManagedObjectContext _createAndPostChangeNotification:deletions:updates:refreshes:deferrals:wasMerge:] + 1736 (NSManagedObjectContext.m:8098)
16  CoreData                      	0x1a1e10e0c -[NSManagedObjectContext _postRefreshedObjectsNotificationAndClearList] + 164 (NSManagedObjectContext.m:7631)
17  CoreData                      	0x1a1e0fad8 -[NSManagedObjectContext _processRecentChanges:] + 100 (NSManagedObjectContext.m:7714)
18  CoreData                      	0x1a1e3563c -[NSManagedObjectContext _coreMergeChangesFromDidSaveDictionary:usingObjectIDs:withClientQueryGeneration:] + 3436 (NSManagedObjectContext.m:3723)
19  CoreData                      	0x1a1e34350 __116+[NSManagedObjectContext(_NSCoreDataSPI) _mergeChangesFromRemoteContextSave:intoContexts:withClientQueryGeneration:]_block_invoke_4 + 76 (NSManagedObjectContext.m:9531)
20  CoreData                      	0x1a1dcdf80 developerSubmittedBlockToNSManagedObjectContextPerform + 156 (NSManagedObjectContext.m:4002)
21  CoreData                      	0x1a1e41a44 -[NSManagedObjectContext performBlockAndWait:] + 216 (NSManagedObjectContext.m:4113)
22  CoreData                      	0x1a1e39880 +[NSManagedObjectContext _mergeChangesFromRemoteContextSave:intoContexts:withClientQueryGeneration:] + 2372 (NSManagedObjectContext.m:9537)
23  CoreData                      	0x1a1e344a0 -[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:] + 292 (NSManagedObjectContext.m:0)

This is of out-of-bound crash when accessing an array. I am suspecting if it is triggered by a Core Data concurrency issue. Did you try to diagnose your app by using the com.apple.CoreData.ConcurrencyDebug launch option? If not yet, you can probably start with it using the following steps:

  1. Open your project with Xcode.
  2. Click the target in the middle of Xcode’s title bar to show the drop list, and then click “Edit Schema...” to show the schema editing dialog.
  3. Add -com.apple.CoreData.ConcurrencyDebug 1 to the “Arguments Passed on Launch” list. Note that the argument has the “-“ prefix.
  4. Run your app and try to reproduce the issue.

If your app hits a concurrency violation, the debugger halts at the following symbol:

 +[NSManagedObjectContext __Multithreading_Violation_AllThatIsLeftToUsIsHonor__].

Also, you might consider providing a full crash report for folks to take a look, which hopefully unveils more information about the crash. you can include crash reports directly in your post using the forums attachment feature

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Core Data crash while trying to merge
 
 
Q