Sync and async code in Swift
Issue #75
We should use DispatchQueue
to build thread safe code. The idea is to prevent two read and write
from happening at the same time from 2 different threads, which cause data corruption and unexpected behaviors. Note that you should try to avoid deadlock https://stackoverflow.com/questions/15381209/how-do-i-create-a-deadlock-in-grand-central-dispatch
All sync
Use try catch
, together with serial queue
. Use sync
function to block current queue.
1 | func getUser(id: String) throws -> User { |
All async
Use Result
, toget with serial queue
. Use async
function to return to current queue.
1 | enum Result<T> { |
Sync read, async write
Use try catch
for read, Result
for write, together with concurrent queue
. Use sync
function for read to block current thread, while using async
function with barrier
flag for write to return to current queue. This is good for when multiple reads is preferred when there is no write. When write with barrier
comes into the queue, other operations must wait.
1 | func getUser(id: String) throws -> User { |
Testing for asynchrony
Before we could use dispatch_apply to submits a block to a dispatch queue for multiple invocations. Starting with Swift, the equivalence is concurrentPerform
1 | DispatchQueue.concurrentPerform(iterations: 1000) { index in |