DEV Community

Khoa Pham
Khoa Pham

Posted on

Sync and async code in Swift

Original post https://github.com/onmyway133/blog/issues/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.

func getUser(id: String) throws -> User {
  var user: User!
  try serialQueue.sync {
    user = try storage.getUser(id)
  }

  return user
}

func setUser(_ user: User) throws {
  try serialQueue.sync {
    try storage.setUser(user)
  }
}
Enter fullscreen mode Exit fullscreen mode

All async

Use Result, toget with serial queue. Use async function to return to current queue.

enum Result<T> {
  case value(T)
  case error(Error)
}

func getUser(id: String, completion: (Result<User>) - Void) {
  try serialQueue.async {
    do {
      user = try storage.getUser(id)
      completion(.value(user))
    } catch {
      completion(.error(error))
    }
  }

  return user
}

func setUser(_ user: User, completion: (Result<()>) -> Void) {
  try serialQueue.async {
    do {
      try storage.setUser(user)
      completion(.value(())
    } catch {
      completion(.error(error))
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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.

func getUser(id: String) throws -> User {
  var user: User!
  try concurrentQueue.sync {
    user = try storage.getUser(id)
  }

  return user
}

func setUser(_ user: User, completion: (Result<()>) -> Void) {
  try concurrentQueue.async(flags: .barrier) {
    do {
      try storage.setUser(user)
      completion(.value(())
    } catch {
      completion(.error(error))
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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

DispatchQueue.concurrentPerform(iterations: 1000) { index in
    let last = array.last ?? 0
    array.append(last + 1)
}
Enter fullscreen mode Exit fullscreen mode

Reference

Top comments (0)