gpt4 book ai didi

ios - Realm 迁移 : Update multiple objects and fallback strategy?

转载 作者:搜寻专家 更新时间:2023-11-01 06:43:16 25 4
gpt4 key购买 nike

我不明白如何迁移多个对象。例如,如何在对象设置和人员上引入字符串“标识符”?

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
}
}
})
}

是否有可能引入一个通用的回退策略:当迁移失败时,模型应该重置?

最佳答案

在迁移 block 中,您只需为 Person 对象添加对 Migration().enumerate(_:_:) 的调用:

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() 抛出错误时删除 realm 文件:

do {
let realm = try Realm()
...
} catch {
let path = RLMRealmConfiguration.defaultRealmPath()
try! NSFileManager.defaultManager().removeItemAtPath(path)
}

关于ios - Realm 迁移 : Update multiple objects and fallback strategy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33451489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com