【发布时间】:2015-07-24 17:51:24
【问题描述】:
我的 Realm 数据库中有 10,000 Person Objects,我只有每个人的 ID。我一直在分析我想出的不同解决方案来更新每个 Person 对象上的所有名称。
在我的测试应用程序中,我使用以下方法检索所有 ID。
下面的代码仅用于获取测试的 ID,在实际应用中我已经有了 ID
var ids = [String]()
for person in realm.objects(Person) {
ids.append(person.id)
}
更新 10,000 个 Person 对象的所有名称的第一种方法
realm.write {
let people = realm.objects(Person).filter("id IN %@", ids)
for person in people {
person.name = "lionpants"
}
}
为 10,000 个 Person 对象更新所有名称的第二种方法
realm.write {
for id in ids {
let person = realm.objectForPrimaryKey(Person.self, key: id)
person?.name = "lionpants"
}
}
令人惊讶的是,这两种方法使用 Xcode Time Profiler 的平均运行时间约为 1200ms(包括获取 ID)(我认为使用谓词的第一种方法会更快)。
就个人而言,由于缺少谓词,我更喜欢第二种方法。但是,当使用 Realm 进行大规模更新时,是否有首选/不同的方法?也许我缺少一些内置函数?
【问题讨论】: