iOS Concurrency with GCD & Operations

Learn how to add concurrency to your apps! Keep your app’s UI responsive to give your users a great user experience, and learn how to avoid common concurrency problems, like race condition, priority inversion and deadlock. By Audrey Tam.

3.8 (4) · 1 Review

Download materials
Save for later
Comments
Share

Learning path

This is part of the Concurrency in iOS learning path. View path.

Who is this for?

Experienced iOS developers, with an intermediate level of knowledge of Swift and iOS development.

Learn how to use dispatch queues and operations in your apps, to keep your app’s UI responsive, avoid data races, priority inversion and deadlock. You’ll learn how to manage groups of tasks with dependencies and how to use asynchronous API methods in dispatch groups and operation queues.

Covered concepts

  • Global Dispatch Queues
  • Serial vs. Concurrent Queues
  • Synchronous vs. Asynchronous Dispatch
  • Dispatch Work Item, Dispatch Group, Dispatch Semaphore
  • Data Races, ThreadSanitizer (TSan) and Thread-safe Classes
  • Quality of Service and Priority Inversion
  • Avoiding Deadlock
  • Operation and OperationQueue
  • Asynchronous Operation
  • Operation Dependencies
  • Canceling Operations

Part 1: Grand Central Dispatch

01
Toggle description

Find out what’s in store for you as you work through this course and why it’s important.

GCD 8:17
Toggle description

Learn about the different Grand Central Dispatch global queues and how they manage threads for you.

Toggle description

Develop your understanding of serial vs. concurrent queues and synchronous vs. asynchronous dispatch.

Toggle description

Learn how to create and execute a dispatch work item if you want to be notified when the task completes, or if you might need to cancel the task.

Practice using dispatch queues to download and display images, through a hands-on challenge.

Toggle description

Learn how to use URLSession dataTask to download images in the background, then dispatch back to the main queue to display them.

Toggle description

Learn how to create a group of slow tasks, run them on a dispatch queue and be notified when they all finish.

Toggle description

Learn how to wrap an asynchronous function so you can run it in a dispatch group, and it tells the group when it really finishes.

Toggle description

In a hands-on challenge, practice wrapping the asynchronous function URLSession dataTask to download a group of images.

Toggle description

Learn how to use a dispatch semaphore to control the number of dispatch group tasks that run at the same time.

Part 2: Concurrency Problems & Solutions

Toggle description

Learn how to prevent the three potential concurrency problems: priority inversion, data races and deadlocks.

Toggle description

Explore the priority inversion problem by creating a situation where a high-priority task must wait for lower-priority tasks.

Toggle description

Learn how to use Xcode’s TSan tool to detect data races, then use a dispatch barrier to make a class thread-safe.

Practice using TSan and making a class thread-safe with a dispatch barrier, through a hands-on challenge.

Part 3: Operations & OperationQueues

Operations 9:06
Toggle description

Get started with Operations by learning how to create and use BlockOperations and what they’re useful for. Learn how to define a reusable operation, with input and output properties, as a subclass of Operation.

Toggle description

Discover the real power of Operations happens when you add them to an OperationQueue and let it handle scheduling and execution.

In a hands-on challenge, practice using an OperationQueue to apply a tilt-shift filter to an array of images.

Toggle description

Find out what you need to do to run an asynchronous operation in an OperationQueue, so the OperationQueue knows when it really finishes. Create an AsyncOperation subclass of Operation to handle Operation state changes, then subclass AsyncOperation to wrap an asynchronous function.

In a hands-on challenge, practice subclassing AsyncOperation to wrap the image download task, then create instances to download an array of images.

Toggle description

Learn how to set up a dependency between two operations so an OperationQueue can run them in the correct order.

Toggle description

In a hands-on challenge, practice defining an Operation dependency to apply the tilt-shift filter after an image finishes downloading.

Toggle description

Learn how to cancel download and filter operations in the sample app, if an image’s cell scrolls off the screen.

Conclusion 1:38
Toggle description

Let’s review everything you’ve learned in this course about Grand Central Dispatch, concurrency problems, Operations and OperationQueues.