我从基于Core Data文档的模板创建了一个新的Swift应用程序。该应用程序运行正常,但对于新版本,我想添加轻量级迁移。
在我读过的核心数据文档中,我只需要在addPersistentStoreWithType:configuration:URL:options:error:
方法中添加一些选项,但实际上没有提示其中调用/添加此方法。
我有Document
类派生自NSPersistentDocument
以及应用程序委托。
addPersistentStoreWithType:configuration:URL:options:error:
在哪里调用? 答案 0 :(得分:2)
它(隐藏)在NSPersistentDocument
的文档中。
您可以通过重写方法NSPersistentDocument和configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions :)来自定义持久性堆栈的体系结构。您可能希望这样做,例如,指定特定的托管对象模型。
覆盖func configurePersistentStoreCoordinator(for url: URL, ofType fileType: String, modelConfiguration configuration: String?, storeOptions: [String : AnyObject]? = [:])
。将您的选项添加到storeOptions
并调用super。
答案 1 :(得分:0)
请参阅apple docs
在Swift中创建选项并调用addPersistentStoreWithType
let options = [NSMigratePersistentStoresAutomaticallyOption:true,NSInferMappingModelAutomaticallyOption:true]
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options)
这是在lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator
appDelegate:(didFinishLaunchingWithOptions)
完成的
编辑说:这仅适用于iOS应用,对于基于文档的应用,您可以找到答案here