gpt4 book ai didi

ios - 如何正确加载数千条记录到Realm?

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:08 27 4
gpt4 key购买 nike

我正在尝试使用 Realm 将大约 8000 条记录保存到磁盘中,但它阻止了 UI。因此,我使用 Realm.asyncOpen 在后台线程中执行数据保存。

当我尝试以这种方式保存大量记录时,问题是 CPU 使用率为 100%。

如何正确加载千条记录到Realm?

最佳答案

尝试官方demo中的方式保存大量数据:

DispatchQueue(label: "background").async {
autoreleasepool {
// Get realm and table instances for this thread
let realm = try! Realm()

// Break up the writing blocks into smaller portions
// by starting a new transaction
for idx1 in 0..<1000 {
realm.beginWrite()

// Add row via dictionary. Property order is ignored.
for idx2 in 0..<1000 {
realm.create(Person.self, value: [
"name": "\(idx1)",
"birthdate": Date(timeIntervalSince1970: TimeInterval(idx2))
])
}

// Commit the write transaction
// to make this data available to other threads
try! realm.commitWrite()
}
}
}

关于ios - 如何正确加载数千条记录到Realm?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45075407/

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