在Swift核心数据应用程序中添加`addPersistentStoreWithType:configuration:URL:options:error:`的位置?

时间:2016-07-27 10:13:19

标签: swift macos core-data swift2 core-data-migration

我从基于Core Data文档的模板创建了一个新的Swift应用程序。该应用程序运行正常,但对于新版本,我想添加轻量级迁移

在我读过的核心数据文档中,我只需要在addPersistentStoreWithType:configuration:URL:options:error:方法中添加一些选项,但实际上没有提示其中调用/添加此方法。

我有Document类派生自NSPersistentDocument以及应用程序委托。

  • 我需要为项目进行轻量级迁移添加什么内容?
  • 方法addPersistentStoreWithType:configuration:URL:options:error:在哪里调用?

2 个答案:

答案 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