Swift Realm 属性“*"已添加到最新的对象模型迁移中 [英] Swift Realm Property '*' has been added to latest object model MIGRATION

查看:32
本文介绍了Swift Realm 属性“*"已添加到最新的对象模型迁移中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 RLMObject 中添加了新的数组属性,并且

I have added new array attribute to the RLMObject and

public class Student: RLMObject {
    dynamic var id = 0
    dynamic var name = ""
    dynamic var resultList = RLMArray(objectClassName:Result.className())
}

public class Result: RLMObject {
}

错误日志:

由于以下原因,对象类型学生"需要迁移错误:- 属性resultList"已添加到最新的对象模型中.

Migration is required for object type 'Student' due to the following errors: - Property 'resultList' has been added to latest object model.

尝试失败:

let configuration:RLMRealmConfiguration = RLMRealmConfiguration.defaultConfiguration()

migration.enumerateObjects(Student.className()) { oldObject, newObject in
    newObject!["resultList"] = RLMArray(objectClassName: Result.className())
}

  let configuration:RLMRealmConfiguration = RLMRealmConfiguration.defaultConfiguration()
    print("Realm db current version: (configuration.schemaVersion)")
    configuration.schemaVersion = 1
    configuration.migrationBlock = {(migration:RLMMigration, oldSchemaVersion: UInt64) in
        print("Realm db migration start")
        if oldSchemaVersion < 1 {
            print("Schema version: 1 - Rename fields")
            migration.enumerateObjects(Student.className()) { oldObject, newObject in
                newObject!["creationDate"] = oldObject!["createdAt"]
                newObject!["modifiedDate"] = oldObject!["updatedAt"]
            }
        }
        print("Realm db migration finish")
    }
    RLMRealmConfiguration.setDefaultConfiguration(configuration)
    let realm = RLMRealm.defaultRealm()

解决方案:

将您的版本更新为 +1

update your version to +1

configuration.schemaVersion += 1

推荐答案

你必须增加你的 schemaVersion 并在你的 RLMRealmConfiguration 上提供一个 migrationBlock>.在那里你可以迁移表.但是在您的特定情况下,您不需要它.可以自动处理属性的添加.你仍然需要一个空块.

You have to incremented your schemaVersion and provide a migrationBlock on your RLMRealmConfiguration. In there you can migrate tables. But you don't need that in your specific case here. The addition of properties can be handled automatically. You'll still need an empty block.

let config = RLMRealmConfiguration.defaultConfiguration()
config.schemaVersion = 1
config.migrationBlock = { (migration, oldSchemaVersion) in
    // nothing to do
}
RLMRealmConfiguration.setDefaultConfiguration(config)

这篇关于Swift Realm 属性“*"已添加到最新的对象模型迁移中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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