【发布时间】:2021-04-24 21:23:26
【问题描述】:
我已经用 Swift 5 编写了这个应用程序,并且已经在 App Store 上线了。
现在,我只想向 coredata 中的一个现有实体添加一个新的单一布尔属性。
为此,我遵循了以下步骤:
- 在 Project.xcdatamodeld 中添加了 version(它在其中自动创建了 Project 2.xcdatamodel 文件)
- 将此版本设置为默认版本
- 为这个新版本文件中的实体添加一个属性。
- 在 AppDelegate 类中向 NSPersistentContainer 添加了属性 shouldMigrateStoreAutomatically 和 shouldInferMappingModelAutomatically。
问题:
在应用程序中,我成功地将数据保存在 coredata 中并从任何 ViewController 中的 coredata 检索,但是,如果应用程序从头开始打开,它找不到以前的文件并重新创建 coredata 文件。
每当我打开应用程序时,它都会在 xCode 控制台中显示错误:
Connecting to sqlite database file at "/dev/null"
AppDelegate 代码:
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "Project")
let description = NSPersistentStoreDescription()
description.shouldMigrateStoreAutomatically = true
description.shouldInferMappingModelAutomatically = true
container.persistentStoreDescriptions=[description]
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container }()
我遵循的教程: Medium
【问题讨论】:
标签: ios swift core-data core-data-migration