Swift 领域 属性 '*' 已添加到最新的对象模型 MIGRATION

Swift Realm Property '*' has been added to latest object model MIGRATION

我已将新的数组属性添加到 RLMObject 并且

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

public class Result: RLMObject {
}

错误日志:

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

configuration.schemaVersion += 1

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

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