loading...

Sync and async code in Swift

onmyway133 profile image Khoa Pham ・2 min read

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)
  }
}

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))
    }
  }
}

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))
    }
  }
}

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)
}

Reference

Posted on by:

onmyway133 profile

Khoa Pham

@onmyway133

My apps https://onmyway133.com/apps/

Discussion

pic
Editor guide