tezvyn:

Grand Central Dispatch (GCD): Your App's Task Manager

AI-drafted, machine-checkedSource: developer.apple.combeginner
Grand Central Dispatch (GCD): Your App's Task Manager

Grand Central Dispatch (GCD) is your app's task manager, letting you run code in the background without freezing the UI. Use it to offload network requests or heavy computations, then update the UI on the main queue.

WHY IT EXISTS To simplify concurrent programming on Apple platforms. Manually creating and managing threads is complex, expensive, and prone to errors. GCD abstracts away thread management, letting developers focus on the tasks to be performed, not the low-level mechanics of how they run in parallel.

THE MENTAL MODEL Think of GCD as a restaurant's head chef. You write down orders (blocks of code) and hand them to the chef. The chef decides which cook (thread) gets which order and when, based on the order's priority (Quality of Service) and which cooking station (dispatch queue) it's for. Simple orders are done one-by-one (a serial queue), while complex meals have multiple parts cooked at once (a concurrent queue).

HOW IT WORKS You submit work items, which are closures in Swift, to dispatch queues. GCD maintains a pool of threads and executes your work on an appropriate thread. There are two main types of queues: serial queues execute one task at a time in FIFO order, while concurrent queues can execute multiple tasks simultaneously. The main queue, used for all UI updates, is a special serial queue. You dispatch work asynchronously (async), which returns immediately, or synchronously (sync), which blocks the current thread until the task completes.

WHEN TO USE IT Use GCD whenever you have a task that could block the main thread and make the UI unresponsive. This includes network requests, reading or writing large files, complex calculations, or image processing. The standard pattern is to dispatch heavy work to a background queue and, upon completion, dispatch a new task back to the main queue to update the UI with the results.

WHEN NOT TO USE IT Never call sync on a queue from a task already running on that same queue, especially the main queue; this is a guaranteed deadlock. For very short, trivial tasks, the overhead of dispatching might be greater than the work itself. For more complex asynchronous flows, especially those requiring cancellation or managing sequences of values, Swift's modern Concurrency features (async/await) are often a better and safer choice.

ONE CANONICAL EXAMPLE A user taps a button to download an image. The button's action handler dispatches a task to a global background queue using DispatchQueue.global().async. This task contains the networking code to download the image data. Once the download is complete, inside that same block, a new task is dispatched back to the main queue using DispatchQueue.main.async. This second task takes the downloaded image data and sets it on a UIImageView, making it visible to the user without ever freezing the interface.

Read the original → developer.apple.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.