小编典典

Swift Realm属性“ *”已添加到最新的对象模型MIGRATION中

swift

我已经向RLMObject添加了新的数组属性,并且

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

public class Result: RLMObject {
}

错误日志:

由于以下错误,对象类型“ Student”需要迁移:-属性“ resultList”已添加到最新的对象模型中。

尝试失败:

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

configuration.schemaVersion += 1

阅读 328

收藏
2020-07-07

共1个答案

小编典典

您必须增加schemaVersion并在migrationBlock上提供RLMRealmConfiguration。您可以在其中迁移表。但是您在这里不需要您的特定情况。属性的添加可以自动处理。您仍然需要一个空块。

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