【问题标题】:Core Data Version Migration with iCloud使用 iCloud 迁移核心数据版本
【发布时间】:2021-08-11 00:13:57
【问题描述】:

我有一个正在生产中的 Core Data 应用程序,并且想向现有实体添加属性。经过研究,我发现这是一个“轻量级迁移”,只需在我的persistentStoreDescriptions 中添加NSMigratePersistentStoresAutomaticallyOptionNSInferMappingModelAutomaticallyOption 选项,然后再加载持久化存储即可:

let container = NSPersistentCloudKitContainer(name: "app")

guard let description = container.persistentStoreDescriptions.first else { fatalError("Container not available") }

//For migration VVV

description.setOption(true as NSNumber, forKey: NSMigratePersistentStoresAutomaticallyOption)
description.setOption(true as NSNumber, forKey: NSInferMappingModelAutomaticallyOption)

//End of migration additions ^^^

container.loadPersistentStores(completionHandler: { (storeDescription, error) in
     if let error = error as NSError? {
          fatalError("Store loading failure")
     }
}

虽然这看起来很容易解决,但我只有两个问题在其他任何地方都找不到答案:

1.对于从现在起数周后更新其应用版本的任何人,我可以为该应用的所有未来版本保留这两个新的迁移选项吗?

2。由于我使用NSPersistentCloudKitContainer,所以在将新数据模型发布到 CloudKit Dashboard 时,用户当前的应用程序版本是否应该包含两个迁移选项?我担心如果 CloudKit 数据模型与他们的应用程序版本不同,它会在同步他们的项目时崩溃他们的应用程序

非常感谢您对此的任何帮助!改变数据模型总是一种可怕的经历,而且我从来没有做对过,总是导致人们的应用程序崩溃。非常感谢您提供的任何帮助。

【问题讨论】:

    标签: swift core-data icloud core-data-migration nspersistentcloudkitcontainer


    【解决方案1】:

    【讨论】:

    • 太棒了!那么向当前数据模型添加属性所需要做的只是添加一个新的数据模型版本并在那里进行编辑?
    • 只要您的更改符合Lightweight migration 的定义,您应该可以接受
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    相关资源
    最近更新 更多