【问题标题】:Realm Migration Fail with Error : Migration Required, or object has already been opened with a different schema version领域迁移失败并出现错误:需要迁移,或已使用不同的架构版本打开对象
【发布时间】:2023-03-04 20:25:01
【问题描述】:

这里是迁移的代码(在 didFinishLaunchingWithOptions 中){

    Realm.Configuration.defaultConfiguration = Realm.Configuration(
        schemaVersion: 3,
        migrationBlock: { migration, oldSchemaVersion in
            // The enumerateObjects:block: method iterates
            // over every 'Person' object stored in the Realm file
            migration.enumerate(User.className()) { oldObject, newObject in

                if oldSchemaVersion < 1 {
                    newObject!["crashTest"] = ""
                }
                if oldSchemaVersion < 2 {

                }
            }
    }) let realm = try!Realm()

这里是错误:

致命错误:“试试!”表达式意外引发错误:错误 Domain=io.realm Code=0 "由于以下原因需要迁移 错误: - 属性 'crashTest' 已添加到最新的对象模型中。” UserInfo={NSLocalizedDescription=迁移是必需的,因为 以下错误: - 属性 'crashTest' 已添加到最新的对象模型中。}:文件 /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/ErrorType.swift , 第 50 行

【问题讨论】:

  • 你检查过你的Realm文件的schema版本是否真的低于3,你在配置中指定的吗?

标签: ios swift realm


【解决方案1】:

如果您只在本地开发,我建议您重置您的领域数据库,而不是进行迁移。您可以通过删除模拟器或设备上的应用程序来重置数据库。或者,您可以尝试使用 NSFileManager 在访问数据库之前删除领域文件。 let defaultPath = Realm.Configuration.defaultConfiguration.fileURL?.path try! FileManager.default.removeItem(atPath: defaultPath!)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多