Ios Realm.io:写/读操作是否在主线程上执行?

Ios Realm.io:写/读操作是否在主线程上执行?,ios,swift,realm,Ios,Swift,Realm,我很好奇这样的读/写操作是否在主线程上执行: try! realm.write { realm.add(myDog) } 这一点很重要,因为我希望在读取或写入内容后直接运行操作 块在与同步调用write()方法的线程相同的线程上执行。换句话说,如果在主线程上调用write(),则块将在主线程上执行 dispatch_async(dispatch_queue_create("background", nil)) { // Some operations in a background

我很好奇这样的读/写操作是否在主线程上执行:

try! realm.write {
   realm.add(myDog)
}

这一点很重要,因为我希望在读取或写入内容后直接运行操作

块在与同步调用
write()
方法的线程相同的线程上执行。换句话说,如果在主线程上调用
write()
,则块将在主线程上执行

dispatch_async(dispatch_queue_create("background", nil)) {

  // Some operations in a background thread ...

  try! realm.write {
    // this block will be executed on the background thread
  }
}
如果要在主线程上执行写操作,可能需要根据需要分派到主线程

dispatch_async(dispatch_queue_create("background", nil)) {

  // Some operations in a background thread ...

  dispatch_async(dispatch_get_main_queue()) {
    try! realm.write {
      // this block will be executed on the main thread
    }
  }
}