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.
Interview question
What is the primary benefit of using Grand Central Dispatch (GCD) in an application?
- a.To simplify complex asynchronous flows that require cancellation or value sequencing.
- b.To gain direct control over thread creation and management for performance optimization.
- c.To prevent the user interface from freezing by moving intensive tasks to background queues.Correct
- d.To guarantee that all code executes in a strictly sequential order, preventing concurrency issues.
Why? this is the answer
The card explicitly states GCD's purpose is to offload tasks that could block the main thread, thus keeping the UI responsive. Option B is incorrect because GCD abstracts thread management, and option A notes that Swift's modern Concurrency features are often better for complex asynchronous flows.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles