Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DispatchQueue types in swift

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)
like image 448
Saeed Alasiry Avatar asked Oct 02 '18 08:10

Saeed Alasiry


People also ask

What is DispatchQueue in Swift?

Dispatch queues are FIFO queues to which your application can submit tasks in the form of block objects. Dispatch queues execute tasks either serially or concurrently. Work submitted to dispatch queues executes on a pool of threads managed by the system.

Is DispatchQueue main serial or concurrent?

The main dispatch queue is a globally available serial queue executing tasks on the application's main thread. As the main thread is used for UI updates it's important to be conscious when executing tasks on this queue.

How many types of threads are there in Swift?

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)

What is DispatchQueue Global () async?

There are background threads for heavy tasks. DispatchQueue. global() runs these kind of tasks in background threads. You can tell the queue about how important your task is, so that DispatchQueue can prioritize your task. You can do this by providing Quality-of-Service information.


1 Answers

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 )

like image 193
Saeed Alasiry Avatar answered Sep 18 '22 21:09

Saeed Alasiry