【问题标题】:Deleting Classes in List<T> during Realm Migration在领域迁移期间删除 List<T> 中的类
【发布时间】:2020-10-30 06:29:38
【问题描述】:

例如。我有旧模型,像这样:

class Foo:Object {
    @objc dynamic var id = ObjectId.generate()
    let bars = List<Bar>()
    
    override class func primaryKey() -> String? {
        return "id"
    }
}

class Bar:Object {
    @objc dynamic var id = ObjectId.generate()
    
    override class func primaryKey() -> String? {
        return "id"
    }
}

以及新型号:

class Foo:Object {
    @objc dynamic var id = ObjectId.generate()
    
    override class func primaryKey() -> String? {
        return "id"
    }
}

迁移代码:

let config = Realm.Configuration(
    schemaVersion: 1,
    migrationBlock: {migration, oldSchemaVersion in
        if oldSchemaVersion < 1 {
            migration.deleteData(forType: "Bar")
        }
    })
let realm = try! Realm(configuration: config)

运行时报错“Table is target of cross-table link columns”。

如果我先跑

let config = Realm.Configuration(
    schemaVersion: 1,
    migrationBlock: {migration, oldSchemaVersion in
        if oldSchemaVersion < 1 {

        }
    })

然后跑了

let config = Realm.Configuration(
    schemaVersion: 2,
    migrationBlock: {migration, oldSchemaVersion in
        if oldSchemaVersion < 1 {

        }
        
        if oldSchemaVersion < 2 {
            migration.deleteData(forType: "Bar")
        }
    })

结果奏效了。

这里的问题是,这是一种将两次迁移合并为一次迁移的方法吗?

【问题讨论】:

    标签: realm-list realm-migration


    【解决方案1】:

    我找到了解决方案。只需将两个迁移一起应用即可。

    let url = Realm.Configuration().fileURL!
    let schemaVersion = try! schemaVersionAtURL(url)
    
    if schemaVersion == 0 {
        autoreleasepool {
            let configuration = 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: 1,
                migrationBlock: { migration, oldSchemaVersion in
                    // We haven’t migrated anything yet, so oldSchemaVersion == 0
                    if (oldSchemaVersion < 1) {
                        
                    }
            })
            _ = try! Realm(configuration: configuration)
        }
        
        autoreleasepool {
            let configuration = 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: 2,
                migrationBlock: { migration, oldSchemaVersion in
                    // We haven’t migrated anything yet, so oldSchemaVersion == 0
                    if (oldSchemaVersion < 1) {
                        
                    }
                    
                    if (oldSchemaVersion < 2) {
                        migration.deleteData(forType: "Bar")
                    }
            })
            _ = try! Realm(configuration: configuration)
        }
    } else {
        let configuration = Realm.Configuration(schemaVersion: schemaVersion)
        _ = try! Realm(configuration: configuration)
    }
    

    autoreleasepool 部分是必需的。否则 schemaVersion 2 将不会被应用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      相关资源
      最近更新 更多