-->

Core Data light migration on OS X

2020-05-03 11:42发布

问题:

I'm using the Xcode 6 document based application with core data template for OS X, which sets up the core data stack behind the scenes (no visible init code). Now I need to perform a simple core data lightweight migration and I have created the new versioned model and activated it. Do I really have to implement the core data stack initialisation by hand just to be able to pass migration permissions? If yes, where should the core data stack be initialised so that it will override the default?

回答1:

You mentioned in a comment that you're using the document-based app template-- which is a crucial detail left out of the original question.

With this template you're using a subclass of NSPersistentDocument. If you want to configure migration with NSPersistentDocument, you need to override configurePersistentStoreCoordinatorForURL:ofType:modelConfiguration:storeOptions:error:. Your implementation would call super's implementation with a different set of options. Something like this:

override func configurePersistentStoreCoordinatorForURL(url: NSURL!, ofType fileType: String!, modelConfiguration configuration: String?, storeOptions: [NSObject : AnyObject]!, error: NSErrorPointer) -> Bool {

    let options = [ NSMigratePersistentStoresAutomaticallyOption : true,
        NSInferMappingModelAutomaticallyOption: true ]

    return super.configurePersistentStoreCoordinatorForURL(url, ofType: fileType, modelConfiguration: configuration, storeOptions: options, error: error)
}