首页 > 解决方案 > 无法在路径“var/mobile/container/Data/Application/XXXX/Documents/default.realm”打开领域

问题描述

我的应用程序现在已经上线,之后我在领域数据库中进行了一些更改,并且我将领域版本从 18 增加到 19,当我在以 19 增加领域版本后运行我的项目时,我得到了如下所示的错误。

Error Domain=io.realm Code=2 
"Unable to open a realm at path '/var/mobile/Containers/Data/Application/D75C5A0A-AAF2-46F5-BDB1-FC738C72EEA4/Documents/default.realm': Invalid top array (ref: 256344, size: 11)

代码

class RealmDB {

    // older version is 18
    fileprivate static let realmVersionn:UInt64 = 19 
    
    static func migrateIfNeeded() {
        
        let config = Realm.Configuration(
            // Set the new schema version. This must be greater than the previously used
            // version (if you've never set a schema version before, the version is 0).
            schemaVersion: realmVersionn,
            
            // Set the block which will be called automatically when opening a Realm with
            // a schema version lower than the one set above
            migrationBlock: { migration, oldVersion in
                // We haven’t migrated anything yet, so oldSchemaVersion == 0
                if (oldVersion < realmVersionn) {
                    // Nothing to do!
                    // Realm will automatically detect new properties and removed properties
                    // And will update the schema on disk automatically
                    print("### Realm DB has migrated.")
                }
        })
        
        Realm.Configuration.defaultConfiguration = config
        
        do {
            let _ = try Realm()
        } catch let error {
            AlertView.show(message: error.localizedDescription) {(_) in}
            print("REALM Error : ",error)
        }
        
    }

}

请不要降级这个问题,我知道以前有人问过这个问题,但我没有从那里找到可靠的解决方案。

如果你在这方面发现了什么,请告诉我。提前致谢。

标签: iosswiftrealmrealm-migration

解决方案


推荐阅读