Core Data light migration on OS X

大兔子大兔子 提交于 2019-12-02 21:34:30

问题


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)
}


来源:https://stackoverflow.com/questions/29770959/core-data-light-migration-on-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!