GCD (Grand Central Dispatch) is a low-level concurrency framework provided by Apple to help you manage multithreading and execute tasks asynchronously or concurrently in iOS/macOS apps.

It lets you run time-consuming tasks in the background (like API calls, file reading, or image processing) while keeping the UI smooth and responsive.

Why Use GCD?

  • Prevents UI from freezing by offloading work to background threads.
  • Makes your app more responsive.
  • Helps you run tasks in parallel.
  • Enables scheduling and delayed execution.

Main Concepts in Grand Central Dispatch

1. Dispatch Queues

A dispatch queue is an object that manages the execution of tasks serially or concurrently.

Queue TypeDescription
Main QueueExecutes tasks on the main thread (UI updates must run here).
Global QueuesConcurrent background queues managed by the system.
Custom QueuesYou create your own serial or concurrent queues.

2. Sync vs Async

MethodBlocks Current Thread?Waits for Task to Finish?
syncYesYes
asyncNoNo

Common GCD Examples in Swift

1. Perform Background Task, Then Update UI

DispatchQueue.global(qos: .background).async { // Background task (e.g., download, compute) DispatchQueue.main.async { // Update UI on main thread }
}

2. Delay Execution

DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { print("Executed after 2 seconds")
}

3. Create a Custom Serial Queue

let serialQueue = DispatchQueue(label: "com.myapp.serial")
serialQueue.async { print("Task 1")
}
serialQueue.async { print("Task 2") // Runs after Task 1
}

4. Create a Concurrent Queue

let concurrentQueue = DispatchQueue(label: "com.myapp.concurrent", attributes: .concurrent)
concurrentQueue.async { print("Task A")
}
concurrentQueue.async { print("Task B") // Can run in parallel with Task A
}

Quality of Service (QoS)

Specifies the priority level of tasks:

QoSUse Case
.userInteractiveFor UI-critical tasks
.userInitiatedHigh-priority tasks initiated by user
.utilityLong-running tasks (e.g., downloads)
.backgroundLowest priority (e.g., syncing data)

When to Use GCD

  • Network/API calls
  • File I/O operations
  • Image processing
  • Timers, animations, progress bars
  • Offloading heavy calculations

Conclusion

GCD helps keep background tasks running with most efficiency. This keeps the iOS app UI responsive and smooth. It simplifies concurrency and improves overall performance. To make the most of it, hire an iOS developer with strong GCD experience.