如何在领域迁移中执行查询?

时间:2016-03-15 16:08:05

标签: ios swift realm

我将字段从链接对象移动到非计算字段

public dynamic var venue: Venue!

现在我需要创建一个迁移来填充此字段,然后使用linkedobjects

计算此更改之前

我的迁移就像这样

if (oldSchemaVersion < 7) {
    migration.enumerate(VenueRoom.className()) { oldObject, newObject in
        let venue = try! Realm().objects(Venue).filter("ANY venueRooms.id = %@", oldObject!["id"]!).first
        newObject!["venue"] = venue
    }
}

但它不能处理以下错误

  

&#34;提供的架构版本0小于上次设置的版本6。&#34; UserInfo = {NSLocalizedDescription =提供的架构版本0小于上次设置的版本6.}:

什么似乎有意义

然后我尝试以下方法来获取具有正确配置的域实例

if (oldSchemaVersion < 7) {
    migration.enumerate(VenueRoom.className()) { oldObject, newObject in
        let configuration = Realm.Configuration(
            schemaVersion: self.schemaVersion,
            migrationBlock: nil
        )
        let realm = try! Realm(configuration: configuration)

        let venue = realm.objects(Venue).filter("ANY venueRooms.id = %@", oldObject!["id"]!).first
        newObject!["venue"] = venue
    }
}

但它被卡在了

let realm = try! Realm(configuration: configuration)

它永远不会从那个陈述中回来,没有错误。

在迁移中执行查询的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

在迁移块中执行查询是not currently supported。在添加支持之前,您将要使用Migration.enumerate(_:_:)枚举给定类型的对象并执行Swift中所需的任何过滤。

相关问题