Core Data Light Migration Crash When Widget is Installed (Error 134100)

I'm experiencing a crash during a lightweight Core Data migration when a widget that accesses the same database is installed. The migration fails with the following error:

CoreData: error: addPersistentStoreWithType:configuration:URL:options:error: returned error NSCocoaErrorDomain (134100)
error: userInfo:
CoreData: error: userInfo:
error: 	metadata : {
    NSPersistenceFrameworkVersion = 1414;
    NSStoreModelVersionChecksumKey = "dY78fBnnOm7gYtb+QT14GVGuEmVlvFSYrb9lWAOMCTs=";
    NSStoreModelVersionHashes =     {
    Entity1 = { ... };
    Entity2 = { ... };
    Entity3 = { ... };
    Entity4 = { ... };
    Entity5 = { ... };
};
    NSStoreModelVersionHashesDigest = "aOalpc6zSzr/VpduXuWLT8MLQFxSY4kHlBo/nuX0TVQ/EZ+MJ8ye76KYeSfmZStM38VkyeyiIPf4XHQTMZiH5g==";
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
        ""
    );
    NSStoreType = SQLite;
    NSStoreUUID = "9AAA7AB7-18D4-4DE4-9B54-893D08FA7FC4";
    "_NSAutoVacuumLevel" = 2;
}

The issue occurs only when the widget is installed. If I remove the widget’s access to the Core Data store, the migration completes successfully. The crash happens only once—after the app is restarted, everything works fine.

This occurs even though I'm using lightweight migration, which should not require manual intervention. My suspicion is that simultaneous access to the Core Data store by both the main app and the widget during migration might be causing the issue.

Has anyone encountered a similar issue? Is there a recommended way to ensure safe migration while still allowing the widget to access Core Data?

Any insights or recommendations would be greatly appreciated.

This seems to be discussed in the following post:

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thank you for your response!

After reviewing this discussion, I still have a few questions regarding the best approach to handle this issue:

  • Do I need and how can I reliably detect that the widget is attempting to access Core Data during migration?
  • What is the recommended way to prevent the widget from accessing Core Data until migration is complete?
  • Does Apple provide any official mechanism to ensure that the widget does not interfere with Core Data migration?
  • Is there a way to delay the widget's execution or data fetching until the migration is complete?

Any guidance on best practices for handling this scenario would be greatly appreciated!

Do I need and how can I reliably detect that the widget is attempting to access Core Data during migration? What is the recommended way to prevent the widget from accessing Core Data until migration is complete?

There is no API for a widget to know the staring and ending points of a lightweight migration happening in the main app, but you know the following and so can probably figure them out with your own mechanism:

  • Your main app knows that a migration will start if a) it is about to do loadPersistentStores(completionHandler:) and b) the store needs to be migrated.

  • Your main app knows that a migration is done when the completion handler of loadPersistentStores(completionHandler:) is called.

  • Your widget knows when it will access the store.

Does Apple provide any official mechanism to ensure that the widget does not interfere with Core Data migration?

No.

Is there a way to delay the widget's execution or data fetching until the migration is complete?

For delaying the widget's execution, no. For fetching, it is your code fetching the data, and so you have the control.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Core Data Light Migration Crash When Widget is Installed (Error 134100)
 
 
Q