iOS Interview Prep 7 — Concurrency Part 2
The purpose of this interview preparation series is to assist you in quickly refining your interview skills and thoroughly preparing for the typical questions asked during an iOS interview. If you find it useful, please leave a comment or tap the like button.
Overview
In part 2, we will delve deeper into GCD to refresh your understanding of the fundamental concepts. GCD, which stands for Grand Central Dispatch, is an extremely popular concurrency technique that finds widespread application in various scenarios. Whether it involves transforming API responses into data models or offloading resource-intensive tasks from the main thread, GCD proves to be an indispensable tool. Mastering GCD is undoubtedly crucial when it comes to iOS interviews, as it ranks among the most significant topics in the field.
Grand Central Dispatch — GCD
GCD is a low-level API that provides a way to run blocks of code concurrently on multicore hardware. It is built on top of threads. Under the hood, it manages a shared thread pool. With GCD, you add blocks of code or work items to dispatch queues, and it’s up to GCD to decide which thread to execute them on.
Quick Recap: Synchronous vs asynchronous
Synchronous and asynchronous tasks refer to different ways of executing code. Synchronous tasks are performed in a sequential and blocking manner. When a synchronous task is initiated, the program halts and waits until the task is completed before moving on to the next line of code. This means that the execution flow is paused until the task finishes.
On the other hand, asynchronous tasks are non-blocking and allow the program to continue executing without waiting for the task to complete. Asynchronous tasks are initiated, and then the program proceeds to execute the next line of code immediately, even if the task is still running. Once the asynchronous task completes, a callback or notification is usually used to handle the results or perform additional actions.
Quick Recap: Serial queue VS Concurrent queue
GCD operates on dispatch queues through a class aptly named DispatchQueue.
You submit units of work to this queue, and GCD executes them in a FIFO order. The key is to choose the right kind of dispatch queue and the right dispatching function to submit your work. There are two types of queus: serial queue and concurrent queue.
A serial queue executes tasks in a sequential order, one after the other. While a concurrent queue allows multiple tasks to be executed simultaneously. Tasks in a concurrent queue can start and run concurrently, without waiting for the previous tasks to finish. Serial queue has a single thread associated with them.
Quick Recap: Main queue vs Global queues
DispatchQueue.main.async {
// Task
}
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
guard let overlayImage = self?.image?.faceOverlayImageFrom() else { return }
// Targeting the main queue and calling async guarantees that
// this new task will execute sometime after the current method finishes.
DispatchQueue.main.async { [weak self] in
self?.fadeInNewImage(overlayImage)
}
}
GCD provides three main types of queues:
- Main queue: Runs on the main thread and is a serial queue. It is responsible for handling UI updates, user interactions, and other tasks that require interaction with the user interface. The main queue ensures that these tasks are executed sequentially and on the main thread, ensuring UI consistency and responsiveness.
- Global queues: Concurrent queues shared by the whole system. Four such queues exist, each with different priorities: high, default, low and background. It provides a way to perform tasks in parallel and offload work from the main queue. Global queues are categorized into different quality-of-service (QoS) classes, such as user interactive, user initiated, utility, and background. These queues allow for concurrent execution of tasks based on their QoS priority.
- Custom queues: Queues you create that can be serial or concurrent. Requests in these queues end up in one of the global queues.