このような読み取り/書き込み操作がメインスレッドで実行されるかどうか知りたいです。
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
}
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加