Concurrency Control


Advertisements


Concurrency is a way to execute more than one thing at the same time. In an application, Concurrency refers to multiple tasks running at the same time/concurrently.

Concurrency can make your application faster, cleaner and gives a better experience to the user. Use of multithreading seems difficult to many developers, but API’s like NSOperation and Dispatch Queues makes it easy to use concurrency and multi-threading in the application.

Grand Central Dispatch

GCD is the most commonly used API to manage concurrent code and execute operations asynchronously at the system level. GCD provides and uses queues of task. These Queues are the data structure that use FIFO terminology, i.e. the task at first place in a queue will be executed first and the task in last of a queue will be executed at last.

For example − Suppose a movie ticket booking line, if you are the last person, you will get ticket last, and if someone comes after you, he will get a ticket after you.

Dispatch Queue

Dispatch queue are an easy way to perform a task asynchronously and concurrently in your application. There are two types of queues −

  • Serial Queues − They store tasks in a series manner and execute one task at a time.

  • Concurrent Queues − They store tasks in a series manner and execute one task at a time.

Some of the functions that are used to create or get queues are −

dispatch_queue_create       
// create a serial or concurrent queue 
dispatch_queue_get_label     
// get the label of a given queue 
dispatch_get_main_queue   
// get the one and only main queue 
dispatch_get_global_queue    
// get one of the global concurrent queues 

Adding Tasks to the Queues

There are two types of functions, which help in adding tasks to the queues. They are −

Synchronous Functions

  • dispatch_once − will submit the task only once it is over the application lifetime.

  • dispatch_sync − will submit a task to the queue and return only when the task is completed.

Asynchronous Functions

  • Dispatch_async − will submit a task and return.

  • Dispatch_after − returns immediately but delays for a specific time.

  • Dispatch_async − returns immediately but the task is submitted multiple times.

Example Code

Following is the example code for adding tasks to the queue.

dispatch_async(dispatch_get_main_queue(), update_block);  
// add update_block to main queue  

dispatch_apply(i, some_queue, work_block)  
// add work_block to some_queue i times  

dispatch_sync(background_queue, blocking_block)  
// add blocking block to background queue and wait for completion

General Example of using a Queue

The following code is an example of using a queue.

dispatch_async(background_queue, ^ { 
   // Perform code that takes long time, here. 
   Dispatch_async(dispatch_get_main_queue(), ^ { 
      //Some other task, generally UI. 
   }); 
}); 

This is all we will do with concurrency control, as it is enough information until you become an intermediate iOS Developer.

Keep practicing the concepts you have learnt and try to modify and integrate them according to your requirement.



Advertisements
E-Books Store