4

As I understand there are 3 types of DispatchQueue in swift:

  • Main (serial) (Main Thread)
  • Global (Concurrent) (Background Threads working in parallel)
  • Custom (Concurrent or serial)

And each one maybe work (asynch or synch)

First question:

Is it main queue working on UI thread only and not working on another thread? If the answer yes , how DispatchQueue.Main.async not blocking UI thread. If the answer No , what is the benefit of using DispatchQueue.global as long as DispatchQueue.Main.async work in another thread.

Second question:

what is deference between DispatchQueue.global (async) and DispatchQueue.global (sync) as long as this queue working Concurrent and where to use each one?

Third question:

what is defference between

  1. (serial and sync)
  2. (Concurrent and async)
Saeed Alasiry
  • 212
  • 3
  • 15

2 Answers2

8

As I understand:

Queue is not Thread

Main and global queue may work in same thread

Dispatched: means put task in queue

If Global queue dispatched in Main queue as sync , the dispatched task will work on same thread of Main queue and dispatched task added to Global queue , And this task will freezing the thread

If Global queue dispatched in Main queue as async , the dispatched task will work on other thread of Main queue and dispatched task added to Global queue , And this task will not freezing the thread

If Main queue dispatched in Main queue as async , the dispatched task will work on same thread of Main queue

If Main queue dispatched in Main queue as sync will make exception because make deadlock

Dispatch.sync: put task in queue and wait it until finish

Dispatch.async: put task in queue and not wait it until finish (The task may work in same thread or in another thread)

  • If task dispatched on Global queue and this accord from Main thread then the task will added to Global queue , and new thread will be create and the task will start working immediately in the new thread

  • If task dispatched on Main queue and this accord from Main thread then the task will added to Main queue , and will not work immediately until older tasks in queue finish working (because Main queue is sequential )

Saeed Alasiry
  • 212
  • 3
  • 15
3

DispatchQueue's do not correspond to a single thread directly. The only restriction is that you are only allowed to access the UI from the main thread, which can be done through DispatchQueue.main. However, there's no guarantee that the system will dispatch your execution block to a specific thread if you call it on a specific queue.

DispatchQueue.async is a non-blocking operation, so you can execute several code blocks asynchronously on the same queue without blocking a specific thread, this is why you should always dispatch operations to the main queue asynchronously, to avoid blocking UI updates, since the main queue is solely responsible for UI related tasks. Calling async on any queue, does not guarantee that the execute will happen on a specific thread (be it background or main), it only guarantees that the operation will be executed in a non-blocking manner.

DispatchQueue.sync is a blocking operation, meaning that while a single sync code block is being executed, no other piece of code can be executed on the specific DispatchQueue, so if you dispatch a code block to the main queue synchronously, you will block UI updates and hence your app will freeze.

Dávid Pásztor
  • 40,247
  • 8
  • 59
  • 80
  • As long as **DispatchQueue.global** is Concurrent , how will be blocked when use it with (sync) . **Concurrent** meaning that each element in queue will work in separate thread – Saeed Alasiry Oct 02 '18 at 09:27
  • @user3141421 there's no such thing as a concurrent `DispatchQueue`. As I've said, you can either dispatch an execution block to a queue synchronously (which will be a blocking operation) or asynchronously (non-blocking, this is what you might refer to as concurrent), but the queue itself won't be concurrent. A queue can execute tasks concurrently if it dispatches the work items to several different threads, but you can block any queue by synchronously dispatching an execution block to it. – Dávid Pásztor Oct 02 '18 at 09:30
  • 1
    I don't agree with you , according to apple documentation: **A dispatch queue can be either serial, so that work items are executed one at a time, or it can be concurrent** – Saeed Alasiry Oct 02 '18 at 09:57
  • @user3141421 that's correct, but a concurrent `DispatchQueue` only means that it has the ability to execute tasks concurrently. It doesn't mean that it can __only execute tasks concurrently__. And dispatching tasks synchronously will make a concurrent queue behave like a serial one until the sync blocking operations finish execution. – Dávid Pásztor Oct 02 '18 at 10:01
  • Is it **serial DispatchQueue** working only synchronously? – Saeed Alasiry Oct 02 '18 at 10:10
  • @user3141421 no, you can still dispatch several tasks to a serial queue asynchronously, but only one of them will be executed a time. However, due to asynchronicity, you cannot predict in what order the dispatched tasks will be executed. – Dávid Pásztor Oct 02 '18 at 10:17