Given that you'd move the Core Data store to a group container, I'd assume that the store will be shared across different processes, such as your main app and its extension (a widget, for example). In that case, I'd like to start with pointing you the following threads:
Core Data Light Migration Crash When Widget is Installed (Error 134100).
CoreData error=134100 Failed to open the store.
These threads made it clear that:
After a user installed your new version app, depending on what extension you have, the app and extension can open and migrate the store simultaneously, and so you will need to implement a mechanism to make sure that the store is only migrated once.
Consider having your main app in charge of the the synchronization, as discussed in Avoid synchronizing a store with multiple persistent containers.
Thank you, this is very helpful.
At the moment, my app does not have any widgets or extensions, but adding them is one of the reasons I want to move the store into an app group container.
From what you’ve described, it sounds like the migration window is where things can become problematic once multiple processes are involved. Even if I do not have extensions today, existing users could upgrade to a version that introduces them, which means I still need to think about co-ordination early.
My initial plan was to introduce widgets and extensions in the same update that performs the store relocation and schema migration. However, given the potential for concurrent access during migration, I am reconsidering that approach.
I am now leaning towards introducing them in a later phase, for example:
Phase 1: store relocation and schema migration
Phase 2: CloudKit adoption
Phase 3: widgets and extensions
Does that sequencing sound reasonable to you, or would you still consider it safe to introduce widgets and extensions alongside the migration?
Regarding your suggested flow:
Combining relocation and migration is fine to me. I'd consider the following flow in your app's launch session:
Load the store with your new version model to your Core Data stack, which kicks off the migration.
After the migration is successfully done, copy the migrated store to your target folder using replacePersistentStore(at:destinationOptions:withPersistentStoreFrom:sourceOptions:type:).
Remove the orginal store using destroyPersistentStore(at:type:options:).
Combining relocation and migration in a single launch makes sense to me.
However, I am slightly concerned about deleting the original store immediately. In case of issues with the new version, or if a user rolls back to a previous version of the app, keeping the original store temporarily would provide a safer fallback.
Would you recommend deferring removal of the original store until a later point, or is immediate cleanup generally considered safe in practice?
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: