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

Vla*_*bir 4 realm ios swift swift3

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

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

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

Yun*_*HEN 7

尝试在官方演示中保存大量数据:

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()
    }
  }
}
Run Code Online (Sandbox Code Playgroud)