CoreData轻量级迁移问题

She*_*tif 5 core-data core-data-migration ios swift

我用 Swift 5 编写了这个应用程序,并且已经在 App Store 中上线。

现在,我只想向一个现有实体添加一个新的单个布尔属性

为此,我遵循了以下步骤:

  1. 新增版本(它在其中自动创建了 Project 2.xcdatamodel 文件)
  2. 将此版本设置为默认版本
  3. 在此新版本文件中向实体添加一个属性。
  4. 将属性shouldMigrateStoreAutomaticallyshouldInferMappingModelAutomatically添加到AppDelegate类中的 NSPersistentContainer 中。

问题:

在应用程序中,我能够成功地将数据保存在 coredata 中并从任何 ViewController 中的 coredata 中检索数据,但是,如果应用程序从头打开,它无法找到以前的文件并重新创建 coredata 文件。

每当我打开应用程序时,它都会在 xCode 控制台中显示错误:

Connecting to sqlite database file at "/dev/null"
Run Code Online (Sandbox Code Playgroud)

应用程序委托代码:

// 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 }()
Run Code Online (Sandbox Code Playgroud)

我遵循的教程:

Ely*_*Ely 1

该错误消息可能表明应用程序不知道在哪里查找或保存数据库文件。该短语"/dev/null"表示在内存存储中。

尝试使用

let description = NSPersistentStoreDescription(url: <- File url to the main database file ->)
Run Code Online (Sandbox Code Playgroud)

而不是当前的实例化。