You've probably heard about Grand Central Dispatch and chances are that you've used it in some of your projects. This series takes a close look at Grand Central Dispatch. Before exploring the API of Grand Central Dispatch, we find out what it is and what problem it solves.

What Is Grand Central Dispatch?

Before you start using Grand Central Dispatch, it's important that you become familiar with the underlying technology. What is Grand Central Dispatch? What problem does it solve? And why should you be using it?

Most modern computing devices are powered by capable, multicore processors. The availability of multiple cores allows these devices to execute work concurrently, that is, at the same time. That enables your phone to download a video in the background while keeping the user interface responsive.

Most applications support a wide range of devices. How can you optimize your application to take advantage of the multicore hardware it runs on? That's where Grand Central Dispatch comes into play.

Apple introduced Grand Central Dispatch almost ten years ago. It first made its appearance in Mac OS X Snow Leopard. Today, it's available on iOS, tvOS, macOS, and watchOS. Grand Central Dispatch makes it easier to write multithreaded code, regardless of the platform or device your application runs on.

Grand Central Dispatch is a technology that is designed to make the execution of tasks on multicore hardware performant and straightforward. It can do this because it operates at the system level. Your application operates in a sandbox, which means that it is unaware of other processes running on the system at the same time. Because Grand Central Dispatch operates at the system level, it has an accurate view of the processes that are running and the resources that are available.

By leveraging Grand Central Dispatch, your application doesn't need to know about the platform or device it runs on. It doesn't need to take other processes competing for the same resources into account. Grand Central Dispatch allows applications to schedule work for execution through an easy to use API. It's the task of Grand Central Dispatch to decide when and how the scheduled work is executed.

Dispatch Queues

An application interacts with Grand Central Dispatch through dispatch queues. As the name implies, a dispatch queue is a queue onto which work can be scheduled for execution. A dispatch queue enqueues and dequeues work in FIFO order, that is, first in, first out. This means that the work that is submitted to a dispatch queue is executed in the order in which it was submitted. We talk more about that in the next episode.

As a developer, you have several options for scheduling work onto a dispatch queue. Submitting a block to a dispatch queue is a common option. In Swift, a block is nothing more than a closure that takes no arguments and returns Void or an empty tuple. This is what that looks like. Don't worry about the syntax for now. We explore the Grand Central Dispatch API in more detail later in this series.

DispatchQueue.main.async {
    print("Hello World")
}

We ask the DispatchQueue class for a reference to the dispatch queue that is associated with the main thread. We explore this in more detail later. We submit a closure to the dispatch queue by invoking the async(execute:) method. This is also known as dispatching work onto the dispatch queue. As I mentioned earlier, the block is a closure that takes no arguments and returns returns Void or an empty tuple.

Managing Threads

Performing work in the background used to be tedious. Creating and managing threads can become complex very quickly. Grand Central Dispatch makes this much easier. It manages a pool or collection of threads and decides which thread is used to execute a block. That's an implementation detail the developer doesn't need to worry about.

Grand Central Dispatch doesn't make a guarantee as to which thread is used to execute a block. There is one exception, though. As I mentioned earlier, Grand Central Dispatch manages a dispatch queue that is tied to the main thread of the application. Work submitted to the main dispatch queue is guaranteed to be executed on the main thread.

Why is that important? The user interface should always be updated on the main thread. If you submit a block in which the user interface is updated to the main dispatch queue, you want the guarantee that the update is performed on the main thread. Grand Central Dispatch provides that guarantee.

More Than Dispatch Queues

Most developers only interact with dispatch queues, but Grand Central Dispatch has a lot more to offer. There are convenient APIs for configuring the work an application schedules onto a dispatch queue and you can take advantage of dispatch groups to manage complexity. Grand Central Dispatch also defines semaphores to make sure you don't run into threading issues.

Apple's concurrency library has matured over the years and it offers a rich API that makes complex tasks trivial. Understanding how Grand Central Dispatch works is essential if your goal is building robust and performant applications.

Why Use Grand Central Dispatch?

As I mentioned earlier, Grand Central Dispatch makes it easier to write multithreaded code. But there's more to the story. It's important to understand why it's beneficial for your application to take advantage of Grand Central Dispatch.

Modern computing devices have a complex architecture and they are incredibly performant. As developers, we sometimes forget that we need to be careful how the resources of these devices are used. Apple's hardware has become so powerful that we often neglect performance or battery life. How should your application respond if the user enables Low Power Mode on their device? Can it even take that into account? As a matter of fact, it can.

Because Grand Central Dispatch operates at the system level, it is aware of the capabilities of the device and its resources. Modern processors are complex pieces of engineering. For example, a processor can prioritize performance over efficiency if energy isn't a bottleneck. But if your phone is running low on energy, the system may decide to decrease performance in exchange for efficiency.

Grand Central Dispatch takes this into account. It offers APIs to inform the system about the importance of the tasks your application is performing. I'd like to end this episode by emphasizing that your application needs to be a good citizen on the platform it runs on. You can accomplish that by not only using Grand Central Dispatch but also by understanding how it works. That is the focus of this series.

What's Next?

Now that you know what Grand Central Dispatch is and how your applications can benefit from it, it's time to start working with dispatch queues. That's the focus of the next episode.