Introduction: What is Grand Central Dispatch (GCD)
According to Apple:
“GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects. Work submitted to dispatch queues are executed on a pool of threads fully managed by the system. No guarantee is made as to the thread on which a task executes.”
In other words, GCD is an API provided by apple to allow you to manage concurrent operations in a smooth way, in order to avoid freezing of your application and keep it always responsive for users.
Types of Queues:
When dealing with GCD, there are 3 types of queues:
1- The Main Queue:
This Queue has the highest priority of all , and it runs on the main thread.
All UI updates should be done on this thread or otherwise lagging and weird crashes will occur in your application.
You use it simply like this:
2- The Global Queue:
This Queue is separated into 4 main types and a default type according to QOS(Quality of Service), from highest priority to lowest:
1- userInteractive : Work is virtually instantaneous. Similar to main thread.
2- userInitiated : Work is nearly instantaneous, such as a few seconds or less.
3- default: don’t use it usually , the type will be inferred by the system
4- utility: Work takes a few seconds to a few minutes.
5- background: Work takes significant time, such as minutes or hours.
Both, utility and background threads should be used for heavy operations that needs time in order not to block the main thread.
You can simply call one as follows:
3- Custom Queues:
Custom queues are queues that you can create on your own and give whatever QOS and attributes you want:
Using Queues Together:
Usually in a real world scenario you often need to use those queues together.
Let us say you need to do a heavy operation like downloading and image and then display this image in an imageView.
You do it as follows:
As you can notice we do the heavy operation that can take a lot of time inside a background thread and then display the image inside an imageView using the main thread.
Async VS Sync:
As you saw in the example above we used the word async after each queue.
With GCD, you can dispatch a task either synchronously or asynchronously.
Sync means that no other task will start until the sync operation finishes , while Async will allow other tasks to start even if it didn’t finish yet.
Let us take a look at real world examples:
Notice in the above example both tasks are async , they were executed at the same time. However the userInteractive thread had more priority of execution than the background thread which is normal as discussed before.
Now let us take a look if the first task was sync what will happen:
As you can see although the userInteractive thread has higher priority it wasn’t executed at all until the first sync task was completed.
Even though the above example is simple, it clearly shows how a program behaves with queues running synchronously and asynchronously , so you should be aware of using sync unless needed , and use async in most of the cases to avoid blocking your application in front of your user.
There are a lot more to know about managing threads in your application to be fit in a single article . But what is important as a beginner to know that not also you should perform slow or expensive processes on asynchronously , you should always perform them on low priority threads ( example: background thread), and always keep in mind to do any UI updates on the main thread.