ios - 迁移可以在没有多个数据模型版本的情况下完成。欢呼?

标签 ios swift core-data migration

我指的是精彩教程 Swift Core Data Tutorial用于核心数据中的迁移。

tut 指出,核心数据的迁移与数据模型的多个版本齐头并进。

我正在试验轻量级迁移,并弄清楚了以下几点:

场景 - 我有一个数据模型,我试图向实体添加一些新属性,但出现错误 -

iOS 版本 - 9.1 Swift 版本 - 2.2

"The model used to open the store is incompatible with the one used to create the store”

然后我尝试在不创建任何新版本的情况下启用迁移,方法是添加代码 -

let options = [ NSMigratePersistentStoresAutomaticallyOption : true, NSInferMappingModelAutomaticallyOption : true ]

try persistentStoreCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: URLPersistentStore, options: options)

然后我尝试以类似的方式更改数据库,即向实体添加属性并欢呼!!,它没有导致崩溃。即使我尝试访问特定的实体和属性,但也成功了。

发生的是没有多个版本的迁移。

问题 - 那么是否可以在没有多个版本的数据模型的情况下进行迁移?

示例代码链接 - https://www.dropbox.com/s/mnb2dzxn56ghuet/CoreDataSwift-NSFetchedResultsController-master-2.zip?dl=0

最佳答案

从 iOS 9 开始,Core Data 会将数据模型复制到持久存储,并在需要和请求轻量级迁移时将其用作起点。因此,即使旧模型版本不可用,也可以进行轻量级迁移。这仅适用于 SQLite 持久存储,并且仅适用于轻量级模型迁移。

这在 What's New in Core Data session 中有描述在 WWDC 2015 上。

关于ios - 迁移可以在没有多个数据模型版本的情况下完成。欢呼?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39207492/

相关文章:

ios - iphone 键盘 unicode 映射

ios - Swift 中的 NSExpression 计算器

ios - 快速创建两个只有底部边框的文本字段

cocoa 绑定(bind)错误

ios - 如何使用 NSManagedObject 的子类?

ios - AVCaptureSession 和相机线程未关闭 [iOS]

ios - 使用 `self.navigationController?.popViewController(animated: true)` 是关闭当前的 ViewController 还是只是返回到前一个?

ios - tableView.moveRow 的自定义动画(位于 : to:)

swift - swift 中 tvOS 的 Toast 功能

ios - swift 错误 : Could not determine generated file paths for Core Data code generation: