Reputation: 2687
I am curious if read / write operations like this are executed on the main thread:
try! realm.write {
realm.add(myDog)
}
This is important because I want to run operations directly after something has been read or written to realm.
Upvotes: 1
Views: 314
Reputation: 10573
Block is executed on the same thread as the thread that calls the write()
method synchronously. In different words, if you call write()
on the main thread, the block will be executed on the main thread.
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
}
}
If you'd like to execute the write operation on the main thread, you may need to dispatch to the main thread as needed.
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
}
}
}
Upvotes: 1