Search code examples
swiftmacoscore-dataswift2core-data-migration

Where to add `addPersistentStoreWithType:configuration:URL:options:error:` in Swift Core Data Application?


I created a new Swift application from the Core Data document based template. The application is working fine, but for a new version I would like to add lightweight migration.

In the core data documentation I read, that I just have to add some options to in the addPersistentStoreWithType:configuration:URL:options:error: method, but where is actually no hint where this method is called/added.

I have the Document class which is derived from NSPersistentDocument and also the application delegate.

  • What do I have to add to the project for lightweight migration?
  • Where is the method addPersistentStoreWithType:configuration:URL:options:error: called?

Solution

  • It's (hidden) in the documentation of NSPersistentDocument.

    You can customize the architecture of the persistence stack by overriding the methods NSPersistentDocument and configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:). You might wish to do this, for example, to specify a particular managed object model.

    Override func configurePersistentStoreCoordinator(for url: URL, ofType fileType: String, modelConfiguration configuration: String?, storeOptions: [String : AnyObject]? = [:]). Add your options to storeOptions and call super.