Grand Central dispatch for concurrency and multitasking in iOS

Posted By : Varun Wadhwa | 12-Jun-2016



Grand central dispatch is low level C api to achieve concurrency,GCD use Dispatch queues to invoke your tasks.

Dispatch queues are the pools of thread managed by GCD.

Further tasks can be excuted in synchronous or 
asynchronous way.

dispatch_sync func allows to execute task on queue in synchronous way.
dispatch_async func is used to excute task asynchronously,

Types of  dispatch queues :-

1. Main queue :

  • Execute all tasks on Main Thread.
  • dispatch_get_main_queue func. to retrieve control of main queue,
  •  All UI- related tasks can be performed by using these queues.

2. Concurrent queue :

  • Execute tasks synchronous/asynchronous ,
  • multiple concurrent can be used to execute multiple tasks in parallel.
  • dispatch_get_global_queue function to retrieve the control of a concurrent queue.      

3. Serial queue :

  • execute tasks in FIFO(first-in-first-out) manner ,
  • dispatch_queue_create to create serial queues. 
  • These queues donot run on Main thread.

Tasks to be executed passed in form of blocks.

example :

dispatch_async(dispatch_get_main_queue(),{
    // task to be executed on main queue asynchronously
}
)

Note :-  dispatch_sync cannot be called on main thread, it'll block main thread and deadlock can be occured.

THANKS

 

About Author

Author Image
Varun Wadhwa

Varun is a mobile developer with experience in various technologies like Titanium , Cordova and native iOS development.

Request for Proposal

Name is required

Comment is required

Sending message..