我已将新的数组属性添加到RLMObject和
public class Student: RLMObject {
dynamic var id = 0
dynamic var name = ""
dynamic var resultList = RLMArray(objectClassName:Result.className())
}
public class Result: RLMObject {
}
错误日志:
对象类型需要迁移'学生'由于以下原因 错误: - Property' 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
答案 0 :(得分:6)
您必须增加schemaVersion
并在migrationBlock
上提供RLMRealmConfiguration
。在那里你可以迁移表。但是你不需要在特定的情况下在这里。可以自动处理属性的添加。你还需要一个空块。
let config = RLMRealmConfiguration.defaultConfiguration()
config.schemaVersion = 1
config.migrationBlock = { (migration, oldSchemaVersion) in
// nothing to do
}
RLMRealmConfiguration.setDefaultConfiguration(config)