【问题标题】:Where to add `addPersistentStoreWithType:configuration:URL:options:error:` in Swift Core Data Application?在 Swift 核心数据应用程序中在哪里添加`addPersistentStoreWithType:configuration:URL:options:error:`?
【发布时间】:2016-12-01 06:59:46
【问题描述】:

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

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

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

  • 我必须在项目中添加什么才能进行轻量级迁移?
  • 方法addPersistentStoreWithType:configuration:URL:options:error:在哪里调用?

【问题讨论】:

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


    【解决方案1】:

    apple docs

    在 Swift 中创建选项并调用 addPersistentStoreWithType

    let options = [NSMigratePersistentStoresAutomaticallyOption:true,NSInferMappingModelAutomaticallyOption:true]
    try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options)
    

    这是在lazy var persistentStoreCoordinator: NSPersistentStoreCoordinatorappDelegate:(didFinishLaunchingWithOptions) 中完成的

    编辑说:这仅适用于 iOS 应用,对于基于文档的应用,您会找到答案here

    【讨论】:

    • 你从哪里得到变量coodinator
    • 看看核心数据模板应用程序,它就在那里;)let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    • 问题是关于“Core Data 基于文档的模板”
    • 啊,好吧,我的错。那么您的问题与http://stackoverflow.com/questions/29770959/core-data-light-migration-on-os-x 重复,Willeke 的回答是正确的
    【解决方案2】:

    它(隐藏)在 NSPersistentDocument 的文档中。

    您可以通过重写方法 NSPersistentDocument 和 configurePersistentStoreCoordinator(for:ofType:modelConfiguration:storeOptions:) 来自定义持久化堆栈的架构。例如,您可能希望这样做,以指定特定的托管对象模型。

    覆盖func configurePersistentStoreCoordinator(for url: URL, ofType fileType: String, modelConfiguration configuration: String?, storeOptions: [String : AnyObject]? = [:])。将您的选项添加到 storeOptions 并致电 super。

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多