Why does Core Data migration run on app launch?

23 views Asked by At

I have successfully done Core Data lightweight model migrations before in my app, but currently I'm struggling to deal with an issue where the migration seems to run as soon as the app is updated and run, even before UIApplication's willFinishLaunchingWithOptions method is called, let alone setting up the Core Data stack. One of my goals in this migration is to fix some bug with a Transformable property, so it's vital that I load up the app's persistent store with the old model initially so I can fix some things before running the lightweight migration. But in my testing, it seems that the SQLite store file is changed to the new model as soon as the updated app is installed and run, which signifies that the lightweight migration has already run before the updated app is up and running.

Some details:

  • my Core Data sqlite store is stored in the application container: `NSURL *containerURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier: groupIdentifier];``
  • I don't use a NSPersistentContainer but add the NSPersistentStoreCoordinator and load the NSPersistentStore manually, using addPersistentStoreWithType:configuration:URL:options:error
  • I first run the app in it's existing state, and note that the SQLite store that is created is using the current Core Data model
  • As soon as I run the updated app in Xcode, with the updated model schema, even if I setup a breakpoint in AppDelegate's willFinishLaunchingWithOptions, I notice that the sqlite store file has already updated to the new model, before any NSPersistentStoreCoordinator is created or a NSPersistentStore is added
  • For testing, I removed any app extensions (like widgets and App Shortcuts), so they don't cause any confusion. Same issue occurs.
  • I created a new Core Data template project (which uses NSPersistentContainer) and ran a similar test there (running the app with original model, then updating the model and running it again), and in this case, it works as expected ... the Sqlite store file still shows the old model until the NSPersistentContainer is loaded

What's the best explanation for this issue, and/or the best way to debug something like this? I'm a bit stumped, and don't know where to look.

0

There are 0 answers