我不明白如何迁移多个对象。例如,如何引入字符串"标识符"对象设置和人物?
func configureRealm(){
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
migration.enumerate(Settings.className()) { oldObject, newObject in
if oldSchemaVersion < 1 {
newObject!["identifier"] = NSUUID().UUIDString
}
}
})
}
是否有可能引入一般的回退策略:当迁移失败时,模型应该重置?
答案 0 :(得分:3)
在迁移块中,您只需为Migration().enumerate(_:_:)
对象添加Person
的调用:
func configureRealm(){
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
migration.enumerate(Settings.className()) { oldObject, newObject in
if oldSchemaVersion < 1 {
newObject!["identifier"] = NSUUID().UUIDString
}
}
migration.enumerate(Person.className()) { oldObject, newObject in
if oldSchemaVersion < 1 {
// change whatever needs to be changed
}
}
})
}
或者你可以在let realm = try Realm()
抛出错误时删除领域文件:
do {
let realm = try Realm()
...
} catch {
let path = RLMRealmConfiguration.defaultRealmPath()
try! NSFileManager.defaultManager().removeItemAtPath(path)
}