avatarXiao.J

Summary

This article is a guide for iOS developers preparing for interviews, focusing on advanced concepts of concurrency with Grand Central Dispatch (GCD) and the importance of understanding synchronous versus asynchronous tasks, serial versus concurrent queues, and the main versus global dispatch queues.

Abstract

The provided content is part of an iOS interview preparation series aimed at refining candidates' knowledge of concurrency, particularly GCD. It emphasizes the significance of GCD in iOS development for handling concurrent operations efficiently, such as data model transformation and offloading tasks from the main thread. The article offers a recap of fundamental GCD concepts, including synchronous and asynchronous execution, serial and concurrent dispatch queues, and the main and global queues with their respective quality-of-service (QoS) classes. It underscores the necessity of mastering GCD for iOS interviews, given its critical role in managing concurrency within the iOS ecosystem.

Opinions

  • The author believes that GCD is an essential tool for iOS developers due to its widespread application in various scenarios.
  • Mastery of GCD is considered crucial for success in iOS interviews, reflecting its importance in the field.
  • The article suggests that understanding the differences between synchronous and asynchronous tasks is vital for effective concurrency management.
  • It is implied that the correct use of serial and concurrent queues is key to optimizing task execution in an iOS application.
  • The author conveys that the main queue is central for UI updates and user interactions, highlighting its role in maintaining UI consistency and responsiveness.
  • Global queues are presented as a system-wide resource for parallel task execution and for offloading work from the main queue, with different priorities catering to various task requirements.
  • The article's inclusion of a recap indicates that it values the reinforcement of foundational knowledge as a stepping stone to more advanced topics in iOS concurrency.

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

https://shakuro.com/blog/introduction-to-ios-concurrency

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.
iOS
iOS App Development
iOS Development
Swift
Interview
Recommended from ReadMedium