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.
Dispatch queues are an integral part of Grand Central Dispatch. In this episode, we cover the fundamentals of dispatch queues. Let's start with an example.
You should now have a fundamental understanding of dispatch queues. A dispatch queue is responsible for managing the execution of blocks of work. Grand Central Dispatch determines which thread is used for the execution of a block of work. Developers unfamiliar with Grand Central Dispatch wrongly assume that a dispatch queue is tied to a particular thread. Remember that Grand Central Dispatch doesn't make a guarantee as to which thread is used for the execution of a block of work submitted to a dispatch queue.
The application we worked with in the previous episodes creates and manages a serial dispatch queue and a concurrent dispatch queue. Creating a dispatch queue manually is fine, but it isn't always necessary.
Grand Central Dispatch manages a handful of dispatch queues your application can use. The main dispatch queue is one of these dispatch queues. In this episode, we find out which dispatch queues Grand Central Dispatch provides and when you should consider using them.
Up until now we submitted blocks of work to a dispatch queue by invoking the async(execute:) method. In this episode, we explore how Grand Central Dispatch handles the execution of a block of work. Work can be executed synchronously or asynchronously. What does that mean? What is the difference? And what are the risks?
You already learned quite a bit about Grand Central Dispatch in this series. Most developers stop once they have a good grasp of the fundamentals. That's unfortunate because Grand Central Dispatch offers a number of more advanced APIs that add even more power to Apple's concurrency library. This episode focuses on dispatch work items.
We ended the previous episode with some bad news. We discovered that the current implementation of the ImageTableViewCell class is leaking DispatchWorkItem instances. Before we implement a solution, I have more bad news. The problem is more complex than it appears. There are several memory issues we need to address. Let's tackle them one by one.
The previous episodes have illustrated that working with the DispatchWorkItem class is a bit more complex than submitting a block of work to a dispatch queue. But you get several benefits in return. Control and flexibility are the most important advantages of the DispatchWorkItem class. In this episode, I'd like to show you a few additional benefits if you choose to work with dispatch work items.
In the introduction of this series, I mentioned that Grand Central Dispatch operates at the system level. It has an overview of the processes running on the system and the resources that are available. When your application submits a block of work to a dispatch queue, it's up to Grand Central Dispatch to decide when that block of work is executed.
Quality of service classes are an integral component of Grand Central Dispatch. You know from the previous episode what they are and how to use them. In this episode, you learn how to efficiently apply quality of service classes.
Apple's concurrency library is sparsely documented and that discourages developers from using the more advanced APIs of Grand Central Dispatch. This episode focuses on one of those more advanced APIs, dispatch groups.
Most developers don't use dispatch groups on a daily basis. They have a limited use, but I want to show you in this episode that the DispatchGroup class can greatly simplify the code you write when used correctly.
Dispatch groups are a bit more advanced, but I hope the previous episodes have shown that they can be incredibly useful to manage complex tasks. The DispatchSemaphore class is another more advanced member of Apple's concurrency library. While there are some similarities between dispatch semaphores and dispatch groups, dispatch semaphores are more powerful and more versatile.
The underlying idea of a dispatch semaphore isn't difficult to understand. Remember that a semaphore is nothing more than a variable that can be incremented and decremented in a thread safe manner. The most challenging aspect of a dispatch semaphore is correctly using it. Working in a multithreaded environment is complex. Semaphores help you manage that complexity.