I would set a symbolic breakpoint on "FetchRequest.update", do some experimenting with your app to try and the overall "stack" you're seeing.
I've made some progress and think I've identified the crash.
I wasn't able to set a symbolic breakpoint on FetchRequest.update()
(I tried a few variations but it never seemed to catch anything) but instead I set one on -[NSFetchedResultsController performFetch:]
as that is just a little further down in the call stack.
The app has some custom Core Data migration functionality that enables adhoc data transformation before continuing with a lightweight migration. When the breakpoint was triggered I noticed that one of the other threads was calling the migration functionality so it got me to take a look, that's where I noticed the following line of code.
// Initalizing a coordinator with the same model more than once results in errors when fetching entities.
let persistentStoreCoordinator = migrationStep.destinationVersionIsCurrent ? appPersistentStoreCoordinator : NSPersistentStoreCoordinator(managedObjectModel: migrationStep.destinationModel)
This happens before triggering the migration and determines the coordinator used during the migration, with appPersistentStoreCoordinator
passed in as an already created NSPersistentStoreCoordinator
.
Originally it was creating a new NSPersistentStoreCoordinator
each time but I changed it to the above, sometimes using the already created NSPersistentStoreCoordinator
, as a result of the error described in the comment. I tested the code path where the new NSPersistentStoreCoordinator
was created and the app crashed with the following errors.
executeFetchRequest:error: A fetch request must have an entity.
warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'Activity' so +entity is unable to disambiguate.
warning: 'Activity' (0x30252c370) from NSManagedObjectModel (0x303131900) claims 'Activity'.
error: +[Activity entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
CoreData: error: +[Activity entity] Failed to find a unique match for an NSEntityDescription to a managed object subclass
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: A fetch request must have an entity.'
*** First throw call stack:
(0x1a0120f20 0x197fce018 0x1a821ad88 0x1a82c0b18 0x1a82110dc 0x1a82870ec 0x1a8286600 0x1a828620c 0x1a493b6e8 0x1a5663f08 0x1a5664368 0x1a40c3b70 0x1a40bf504 0x1a40bf320 0x1a40bf294 0x1a40bf224 0x19ea10068 0x1a4139688 0x1a41373bc 0x1a40e9b30 0x1c8d41010 0x1c8d40bfc 0x1c8d3acc0 0x1c8d3a854 0x1a40f8810 0x1a40f8568 0x1a40e9b30 0x1c8d41010 0x1c8d40bfc 0x1c8d407d8 0x1a41536f8 0x1a415288c 0x1a415164c 0x1a414fd7c 0x1a414fb0c 0x1a4064c6c 0x1a232ea4c 0x1a178d3b4 0x1a178cf38 0x1a17e80e0 0x1a175d028 0x1a24e3678 0x1a0102c9c 0x1a00f0dec 0x1a00f0498 0x1a00efcd8 0x1e4fa01a8 0x1a272890c 0x1a27dc9d0 0x1a42e0148 0x1a428c714 0x1a42984d0 0x100441530 0x1004415e0 0x1c37a1e4c)
libc++abi: terminating due to uncaught exception of type NSException
I'm not sure why I left in the code path to create a new NSPersistentStoreCoordinator
but it meant that if a user was updating their app and there was more than one migration to apply, it would create a new NSPersistentStoreCoordinator
and the app would crash. Changing this to always use the already created NSPersistentStoreCoordinator
(appPersistentStoreCoordinator
) resolves the issue.
I would have expected to see some reference to the migration functionality in one of the other threads in the crash log but perhaps it's because the migration was already complete and the crash occurred on the next fetch. Hopefully this fixes the crash I've been seeing in the crash reports.
Thanks for your help. I'm not sure I would have stumbled across the code causing the crash if it hadn't been for your suggestions.