Sobes.tech
Junior — Middle

How do the Async and Concurrent queues fundamentally differ and in what applications are they used?

sobes.tech AI

Answer from AI

In iOS, queues are usually understood as DispatchQueue objects from GCD (Grand Central Dispatch). The main difference between asynchronous (async) and concurrent queues is how they handle tasks:

  • Concurrent Queue allows multiple tasks to run simultaneously. Tasks start in the order they are added but execute in parallel, and their completion order may differ from their start order.

  • Async (asynchronous) call is a way to add a task to a queue that does not block the current thread. An asynchronous call can be made to both serial and concurrent queues.

Thus, "async" is a way to add a task, and "concurrent" is a type of queue. For example, you can asynchronously add a task to a serial queue, and it will execute in order without blocking the calling thread.

Example:

let concurrentQueue = DispatchQueue(label: "com.example.concurrent", attributes: .concurrent)

concurrentQueue.async {
    print("Task 1")
}
concurrentQueue.async {
    print("Task 2")
}

Tasks 1 and 2 can run in parallel.

If the queue were serial (without .concurrent), tasks would execute sequentially but asynchronously, without blocking the calling thread.