gpt4 book ai didi

ios - iOS 13 Core Data 持久化存储迁移过程中出现错误

转载 作者:搜寻专家 更新时间:2023-11-01 05:31:35 25 4
gpt4 key购买 nike

将 XCode 更新到版本 11 后,我向 Core Data 添加了一个新模型版本,在新版本中,我向实体添加了一个新属性。激活新版本并将新属性添加到托管对象文件。

在向用户发布此版本后,它开始崩溃并显示以下消息:“用于打开持久存储的托管对象模型版本与用于创建持久存储的版本不兼容。”和“重复的列名称 ZNEWCOLUMN”。到目前为止,我对 Core Data 模型进行了大量更改,并且迁移始终有效。

此崩溃仅出现在 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 error code:1, 'duplicate column name: ZNAME_OF_THE_COLUMN .... Error Domain = NSCocoaErrorDomain Code = 134110 "An error occurred during persistent storage migration."

我像@iOS Dev 帖子一样加载数据。我在迁移前后检查了模拟器路径中的 xxxx.sqlite 数据库文件,发现没有具有这些新名称的列。要了解 emulator 中 *.sqlite 的路径,您必须放置一个断点,并在它停止时将其放入控制台 po NSHomeDirectory()。然后转到 Finder 窗口,点击 Control + Command + G 键并粘贴路线。你可以使用 DB Browser for SQLite 程序处理它(例如),它是免费的。

经过长时间的搜索,我看到一些人发生了什么,但我没有看到任何解决方案。

我的是:

  1. 选择实际的 *.xcdatamodel。
  2. 选择编辑器 > 添加模型版本。
  3. 提供基于先前模型的版本名称(如 XxxxxxV2.xcdatamodel)。
  4. 点击这个新版本模型NewV2.xcdatamodel。
  5. 在 IDE 右侧的“属性”中选择此新版本作为当前
  6. 在 DDBB 进行更改。
  7. 运行应用程序并将正常工作。

我做了覆盖应用程序的测试(使用新值)并且没问题。

希望这对您有所帮助。

关于ios - iOS 13 Core Data 持久化存储迁移过程中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58215343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com