【问题标题】:Trying to update my DBModel with lightweight migration but nothing happens尝试使用轻量级迁移更新我的 DBModel,但没有任何反应
【发布时间】:2019-02-01 22:16:51
【问题描述】:

所以我的数据库模型中的类别得到了更新(之前有 4 个,现在是 7 个),我想用新对象更新核心数据。

我试图应用这个:https://developer.apple.com/documentation/coredata/using_lightweight_migration

    lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = {
    // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
    // Create the coordinator and store
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.appendingPathComponent("x.sqlite")
    let options = [NSMigratePersistentStoresAutomaticallyOption: true, NSInferMappingModelAutomaticallyOption: true]
    var failureReason = "There was an error creating or loading the application's saved data."
    do {
        try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
    } catch {
        // Report any error we got.}

但是我的数据是一样的……它没有随着新的类别而更新。 有什么帮助吗?谢谢.. PS:要不要做一个新模型,把旧模型映射到新模型?

【问题讨论】:

  • 您需要添加一个新的模型版本,然后选择该模型版本作为当前模型版本。
  • 除了我做了什么?
  • 我的评论有意义吗? @MohamedLee

标签: ios swift core-data core-data-migration lightweight-processes


【解决方案1】:

您只是忘记添加另一个模型,如果您只有一个模型,则无法在模型之间进行迁移。所以只需选择您的 xcdatamodel 然后打开编辑器 -> 添加模型 版本 然后转到文件检查器并选择新的 xcdatamodel 作为当前,然后你得到结果。祝你好运亲爱的朋友

【讨论】:

    【解决方案2】:

    您需要创建 CORE DATABASE 的 DB 模型版本。您可以按照创建数据库版本控制的步骤进行操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-17
      • 2017-05-13
      • 2012-04-05
      • 2022-11-24
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多