I am new to the Today extension and using an embedded framework.
Our app currently uses core data backed by sqlite. If I want to share this between the app and the today extension, should I move this to a framework to be shared by both?
How can I migrate our current version in the app store to be able to upgrade to a new structure?
In case someone wants the solution in swift just add below function in didFinishLaunchingWithOptions.
My PersistentStoreCoordinator Looks like this:
This is the case when there is already an app in appstore and you want to migrate the coreData persistent store file from your default store location to your App Group Location.
Edit : For Deleting the file from the old location it is recommended that we use NSFileCoordinator to perform the task.
Please note the reason why NSFileCoordinator is used to delete the file is because NSFileCoordinator allows us to ensure that file related tasks such as opening reading writing are done in such a way that wont interfere with anyother task on the system trying to work with the same file.Eg if you want to open a file and at the same time it gets deleted ,you dont want both the actions to happen at the same time.
Please call the above function after the store is successfully migrated.
For the migration of old data part, I've done some work.
how to check old db exist or not, I use below code to check.
Migrate old data to new data store place.
If the old data store exist, and the group data store not exist, I use below code do the migrate:
the needMigrate flag is set by checking if old data exist.
For your reference, I paste full code here:
For Migration in swift 3.0 and above just replace below persistentStoreCoordinator method with yours
You need to make sure both the model and the persistent store file are available to both the app and the extension.
For the model, moving it to a framework is a good idea since it means there's only one copy of the model file. As long as both the app and the extension link to the framework, it'll be available to both. If you do that, it's probably a good idea to put the code that sets up the Core Data stack in the framework too, since it'll be the same in both cases.
You can of course just include the model in both targets. That will mean you'll have two copies of the file, which wastes space. Probably not a lot of space, though.
For the persistent store, you'll have to set up an app group and use a store file in the group directory. App groups is one of the settings in "capabilities" for the app and extension-- turn it on and create a group name. Then put the persistent store file in the group directory, which you can find using code like
[I cover some of this in more detail at my blog].
If you have existing data, you'll have to move it to the new store file. This would look something like
migratePersistentStore:toURL:options:withType:error:
to move it to the new location. Then remove the old copy.