Redeem Now
Read More
Read now

Grand Central Dispatch (GCD) in Swift

Save

Grand Central Dispatch (GCD) is used for managing concurrent operation, it is a low-level API. It was first introduced in iOS 4. The main purpose of Grand Central Dispatch is to manage the heavy tasks in the background.

 

First, we will see what is a queue, a queue is a block of code that is managed by the operating system to execute it synchronously or asynchronously either on the main thread or background thread. A queue follows the “FIFO” order. The queue can be either Serial or Concurrent. 

Serial:-  In the serial, the work item will not start the execution until the previous one is finished.

Concurrent:- In this, the execution occurs parallelly.

Let start with Dispatch Queue

First, we create a dispatch queue.

For label a reverse-DNS naming style (com.example.myqueue) is recommended. Let create a method and on which we create our  Dispatch Queue.

The first For loop will execute on the background thread and the last one will execute on the main thread. If we call the myFirstQueue method then it prints “1 2 3 4 5 6 7 8 9 10 20 21 22 23 24 25 26 27 28 29 30”.  In the above method, we have used synchronous execution, so unless the queue code is not finished it will not execute the main thread code.

Now we will execute the code asynchronously and see what will happen.

We will see that the code on the main thread and the code in the dispatch queue will execute parallelly.

Quality Of Service (QoS)

To determine the priorities of the task we used the QoS class property. It is done so that system will know which task has higher priority. So that the execution has to be done accordingly. Although the task runs on the main thread has the highest priority since it makes UI responsive.

The below-listed QoS case is set on the order of its priority. The first one has the highest priority while the last one has the lowest. You can find the documentation from here.

Now we see some example of QoS

In the above example, the priority of “firstQueue” is higher than “secondQueue”. When we call the exampleQoS method then we can see the output from the below image.

We can see clearly that the priority of “firstQueue” is higher than “secondQueue” so it executes faster.

Concurrent Queues

So, far we have seen that our queue is serial, which means if we assign more than one task to a single queue then it is executed one after another. Now we will see how they will run together. In another word, we will see a concurrent queue.

Let first see one example

Output

 

From the above screenshot, we can see when we call the “exampleQueue” method, then we can see all the tasks execute in a serial manner.

Now we will add the extra argument “attributes”  with value concurrent, our queue is now concurrent, when attributes are not present then it is serial. Let see its example.

When we run the above method “concurrentQueueExample” then we can see that the task will execute simultaneously. Also, change in QoS will also affect the task execution. The attributes also take another value “initiallyInactive“. When we pass “initiallyInactive” the task does not start automatically, it needs to trigger to start the execution.

Let see an example.

When the above method is called then it crashes. We need to activate it first. For this, we will use activate() method.

Note:- We have not passed the concurrent value in attributes, so it will execute in a serial manner. To make it concurrent we will pass both the “initiallyInactive” and “concurrent” in the attributes.

example

 

Delaying the Execution

Now, we see how we delay the execution of work. Let see by an example

Output

We can also use DispatchTimeInterval for providing the delay time. We can use seconds, microseconds, nanoseconds, and milliseconds. Let take an example of it.

Main and Global Queues

So, Far we are creating a custom queue, but the system creates a collection of background queues which is known as global queues. If we don’t want to use a custom queue then we can use global queues.

Example

We can also specify the quality of service that we want to use.

Example

Now we will see how to access the main queue. The main purpose of the main queue is for UI update, All the UI updating parts are done on the main thread. And the task which required more time is done on the background thread.

 

DispatchWorkItem

When we don’t want to use the code block then at the time we used DispatchWorkItem. A contain codes of DispatchWorkItem can be executed on a background thread or main thread.

Let see an example of it.

Thanks for reading the Grand Central Dispatch blog.

You can also check other blogs from here. if you have any issue or suggestion you can leave your query/suggestion in the comment section.

Reference

You can check more information from Appcoda

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close