将 XCode 更新到版本 11 后,我向 Core Data 添加了新的模型版本,并在新版本中向实体添加了新属性。使新版本处于活动状态并将新属性添加到托管对象文件中。
向用户发布此版本后,它开始崩溃并显示以下消息:“用于打开持久存储的托管对象模型版本与用于创建持久存储的托管对象模型版本不兼容。" and "重复的列名 ZNEWCOLUMN“。到目前为止,我对核心数据模型进行了很多更改,并且迁移始终有效。
此崩溃仅出现在 iOS 13 上!
这就是我加载核心数据的方式:
lazy var managedObjectContext: NSManagedObjectContext = {
return self.persistentContainer.viewContext
}()
lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded 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.
*/
let container = NSPersistentContainer(name: "MyModel")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
let description = NSPersistentStoreDescription()
description.shouldInferMappingModelAutomatically = true
description.shouldMigrateStoreAutomatically = true
container.persistentStoreDescriptions.append(description)
return container
}()
任何帮助,将不胜感激。
同样的事情也发生在我身上,iOS 12 的轻量级迁移在真实设备和模拟器上是正确的,但在 iOS 13 上失败并出现下一个日志结果:
SQLite错误代码:1,'重复的列名:ZNAME_OF_THE_COLUMN ....错误域
= NSCocoaErrorDomain 代码 = 134110 “持久存储迁移期间发生错误。”
我像@iOS Dev post 一样加载数据。
我在迁移之前和之后检查了模拟器路径中的 xxxx.sqlite 数据库文件,并且没有具有这些新的相同名称的列。
了解*.sqlite的路径emulator你必须设置一个断点,当它停止时放入控制台po NSHomeDirectory()
。
然后转到 Finder 窗口,点击按键Control + Command + G
并粘贴路线。你可以处理它(例如)SQLite 数据库浏览器程序,它是免费的。
经过长时间的搜索,我看到了一些人发生了什么,但我没有看到任何解决方案。
我的是:
- 选择实际的 *.xcdatamodel。
- 选择编辑器 > 添加模型版本。
- 提供基于先前模型的版本名称(例如 XxxxxxV2.xcdatamodel)。
- 单击这个新版本模型NewV2.xcdatamodel。
- 选择这个新版本作为Current在 IDE 右侧的属性上。
- 在 DDBB 做出改变。
- 运行应用程序并且可以正常工作。
我做了覆盖应用程序的测试(使用新值),效果很好。
我希望这会有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)