NSFetchedResultsController index out of bounds during context merging changes

I've noticed several crashes that look like they're caused by an index out of bound in internal methods of NSFetchedResultsController. This happens while changes are merged from the persistent store container into the view context. Here's an example of the last exception backtrace.

Exactly which internal methods that are called in - [NSFetchedResultsController(PrivateMethods) _core_managedObjectContextDidChange:] vary between crash reports but they all end up crashing from _NSArrayRaiseBoundException.

The Core Data stack consists of one persistent store, one persistent store coordinator that the view context is set up to automatically merge changes from, and data is saved to disk from background context.

persistentContainer.loadPersistentStores(...)

viewContext = persistentContainer.viewContext
viewContext.automaticallyMergesChangesFromParent = true
        
backgroundContext = persistentContainer.newBackgroundContext()
backgroundContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        
backgroundClientContext = persistentContainer.newBackgroundContext()
backgroundClientContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

Does anyone have any ideas what could be causing this? Thankful for any ideas or advice on how to investigate further.

The Core Data stack looks fine. Did you implement any NSFetchedResultsControllerDelegate method? For debugging purpose, you can try the following and see if the crashes are still there:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

NSFetchedResultsController index out of bounds during context merging changes
 
 
Q