通过添加List()和另一个模型类来执行迁移 [英] Perform migration by adding List() and another model class

查看:91
本文介绍了通过添加List()和另一个模型类来执行迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号

class Area: Object {

// Specify properties to ignore (Realm won't persist these)

//  override static func ignoredProperties() -> [String] {
//    return []
//  }

    dynamic var id = 0
    dynamic var name = ""

    override static func primaryKey() -> String? {
        return "id"
    }

}

class Region: Object {

// Specify properties to ignore (Realm won't persist these)

//  override static func ignoredProperties() -> [String] {
//    return []
//  }

    dynamic var id = 0
    dynamic var name = ""

    override static func primaryKey() -> String? {
        return "id"
    }

}

我想将let areas = List<Area>()添加到Region类并将dynamic var region: Region?添加到Area类,请问如何执行迁移块?因为迁移文档中的示例仅演示原始类型.

And I would like to add let areas = List<Area>() to Region class and dynamic var region: Region? to Area class, may I ask how to perform the migration block? Cause the example in the migration documentation is only demo-ing the primitive types.

推荐答案

在收到澄清后进行了编辑

好的.因此,由于您确实希望在将areas添加到模型中时对其进行预填充,因此您毕竟需要在迁移块中实现一些逻辑.

Alrighty. So since you do want to pre-populate areas when you add it to your model, you will need to implement some logic in your migration block after all.

let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
    migration.enumerate(Region.className()) { oldObject, newObject in
        if oldSchemaVersion < 1 {
            let areas = newObject?["areas"] as? List<MigrationObject>
            // Add new objects to 'areas' as needed
        }
    }
}

There's some sample code showing how to handle List objects in migrations in Realm Swift's sample code collection

如果向Area添加region属性的目的是要找出该Area是哪个Region对象的子对象,则无需将其实现为模型属性.相反,您可以使用linkingObjects(_: forProperty: )让Realm代表您进行工作.

If your goal in adding a region property to Area is so you can find out which Region object this Area is a child of, then you don't need to implement that as a model property. Instead, you can use linkingObjects(_: forProperty: ) to have Realm work that out on your behalf.

class Area: Object {
    dynamic var id = 0
    dynamic var name = ""
    var regions: [Region] {
        return linkingObjects(Region.self, forProperty: "areas")
    }

    override static func primaryKey() -> String? {
        return "id"
    }
}

要确认我在评论中所说的,迁移是单向的.它们不能降级到以前的架构版本.如果您想快速调试Realm文件上的迁移过程,建议您将原始Realm文件放在一边并处理副本.

To confirm what I said in the comments, migrations are a one-way path. They cannot be downgraded to previous schema versions. If you want to rapidly debug the migration process on a Realm file, I recommend putting the original Realm file aside and working on copies.

原始答案

您实际上是否要添加到这些新属性的任何数据?由于它看起来不像您,因此您不需要在迁移块中实现任何代码.

Do you actually have any data you wish to add to these new properties? Since it doesn't look like you do, you shouldn't need to implement any code in the migration block.

只需增加Realm模式版本号,并提供一个空的迁移块.

Simply increase the Realm schema version number, and supply an empty migration block.

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

})

Realm.Configuration.defaultConfiguration = config

虽然迁移块不能为零,但是如果旧的Realm文件中有任何要在迁移期间进行操作的数据(即,将其移动到另一个属性),则只需在其中放置代码.如果要添加全新的属性,则无需在迁移块内对其进行任何操作.

While the migration block cannot be nil, you only need to put code in there if there's any data in an old Realm file that you want to manipulate during a migration (i.e., moving it to another property). If you're adding brand new properties, it's not necessary to do anything to them inside the migration block.

进入Realm迁移的心态需要一些时间,但是值得庆幸的是,一旦您这样做,您就会意识到它们比您想象的要容易. :)

It takes a little while to get into the mindset of Realm migrations, but thankfully once you do, you realise they're easier than you thought. :)

(免责声明:我为Realm工作,但我在自己的发货iOS应用程序中使用了它,此时我已经在真实用户数据上进行了多次迁移.:))

这篇关于通过添加List()和另一个模型类来执行迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆