【问题标题】:CoreData Lightweight migration issueCoreData 轻量级迁移问题
【发布时间】:2021-04-24 21:23:26
【问题描述】:

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

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

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

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

问题:

在应用程序中,我成功地将数据保存在 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


    【解决方案1】:

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

    尝试使用

    let description = NSPersistentStoreDescription(url: <- File url to the main database file ->)
    

    而不是当前的实例化。

    【讨论】:

    • 从哪里获取文件地址?我在实时应用中使用了“let container = NSPersistentContainer(name: "Project")”。
    • 我认为是这样的:FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).last?.appendingPathComponent("Name Of Your App").appendingPathComponent("Project.sql")。在 Xcode 的 Window 菜单下的“Devices and Simulators”中对您的应用程序的工作版本执行“Download Container”操作,并使用“Show Package Contents”上下文菜单项调查 Finder 中的实际路径。
    • 我什至尝试使用“NSPersistentContainer.defaultDirectoryURL()”,但它使应用程序崩溃。
    • “FileManager.default.urls(用于:.applicationSupportDirectory,在:.userDomainMask)。”关于此评论 > 它正在迁移到新模型,但更新后我丢失了所有数据
    • 还要确保您的新布尔属性是可选的或具有默认值。否则它与轻量级迁移不兼容。
    【解决方案2】:

    您需要指定实际模型的数据库位置的 URL。

     guard let storeURL = try? FileManager
       .default
       .url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
       .appendingPathComponent("yourModel.sqlite") else {
          fatalError("Error finding Model from File Manager")
        }
        
        let container = NSPersistentContainer(name: yourModelName, managedObjectModel: yourModelInstance)
        
        let description = NSPersistentStoreDescription(url: storeURL)
        description.type = NSSQLiteStoreType
        description.shouldMigrateStoreAutomatically = true
        description.shouldInferMappingModelAutomatically = true
        container.persistentStoreDescriptions = [description]
        
        container.loadPersistentStores(completionHandler: { storeDescription, error in
    

    【讨论】:

    • 你救了我,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    相关资源
    最近更新 更多