Version Second Edition
Platform iOS 13
Language Swift 5.1
Editor Xcode 11

Dive Into Concurrency in Your iOS Apps!

Learn what is Concurrency and why would you even want to utilize it in your apps? Learn about Grand Central Dispatch, Apple’s implementation of C’s libdispatch, also known as GCD, as it’s one of the simplest ways to queue up tasks to be run in parallel. Then, take on Operations & Operation Queues for when GCD doesn’t quite cut it; you’ll learn how to further customize and reuse your concurrent work. You’ll then learn common concurrency problems that you could face while developing concurrent applications, such as Race Conditions, Deadlocks, and more. Finally, understand threads and thread sanitizer and the various threading-related concepts and how these connect to the knowledge you’ve accumulated throughout this book. You’ll also learn how to use Thread Sanitizer to ease your debugging when things go wrong.

Before You Begin

This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more.

i

iii

Section I: Getting Started with Concurrency

In this part of the book, you’re going to learn about the basics of Concurrency. You’re going to learn what it is, what kind of problems it solves, and why would you even use it?

Further, you will learn the basic pieces of which Concurrency comprises in iOS development: Grand Central Dispatch and Operations.

This section will provide you with the foundational knowledge regarding Concurrency, so be sure to read through! The upcoming sections will dive much deeper into each of these concepts individually.

Get a quick overview of what concurrency is and why you might want to use it.

1

Concurrency can be handled by either Grand Central Dispatch (GCD) or Operations. Learn about the differences.

2

Section II: Grand Central Dispatch

In this section, you’ll take a deep dive into Apple’s most popular and easy-to-use mechanism to write and manage concurrent tasks — Grand Central Dispatch. You’ll learn how to utilize queues and threads to control the execution of tasks in your app, as well as how to group these tasks together. You’ll also learn about common pitfalls and dangers of using concurrency, and how you can avoid them.

This chapter teaches you how to use a GCD queue to offload work from the main thread. You'll also learn what a "thread" is.

3

In the previous chapter you learned about how queues work. In this chapter you'll expand that knowledge to learn how to submit multiple tasks to a queue which need to run together as a "group" so that you can be notified when they have all completed. You'll also learn how to wrap an existing API so that you can call it asynchronously.

4

By now you know how GCD can make your app so much faster. This chapter will show you some of the dangers of concurrency if you're not careful, and how to avoid them.

5

Section III: Operations

Even though Grand Central Dispatch provides most of the concurrency capabilties you’ll need right out-of-the-box, sometimes you’ll want some extra customizability. This is where Operations come into play. This section will teach you about Operations, Operation Queues, and everything in between.

In this chapter you'll switch gears and start learning about the Operations class, which allows for much more powerful control over your concurrent tasks.

6

Similar to the Dispatch Queues you learned about back in Chapter 3, the Operation class uses an OperationQueue to perform a similar function.

7

Now that you can create an Operation and submit it to a queue, you'll learn how to make the operation itself asynchronous. While not something you'll do regularly, it's important to know that it's possible.

8

The "killer feature" of Operations is being able to tell the OS that one operation is dependent on another and shouldn't being until the dependency has finished.

9

There are times when you need to stop an operation that is running, or has yet to start. This chapter will teach you the concepts that you need to be aware of to support cancelation.

10

Section IV: Real-Life Concurrency

To wrap up this book, this section will be dedicated to showing you how all of the knowledge you’ve accumulated throughout this book could be used for some real-life purposes.

In this section, you’ll take a deeper dive into a common case where concurrency plays a huge role — Core Data — as well as learn about Apple’s Thread Sanitizer as a great tool to debug and resolve concurrency issues and confusions.

Core Data works well with concurrency as long as you keep a few simple rules in mind. This chapter will teach you how to make sure that your Core Data app is able to handle concurrency properly.

11

Data races occur when multiple threads access the same memory without synchronization and at least one access is a write. This chapter will teach you how to use Apple's Thread Sanitizer to detect data races.

12

Meet the team

Contributors

Who is this book for

This book is for intermediate iOS developers who already know the basics of iOS and Swift development but want to learn how to make their app efficiently perform tasks without affecting performance, and how to properly divide work to utilize hardware to the fullest extent.

Concepts covered in this book

  • Grand central dispatch
  • Operations and operation queues
  • Common concurrency problems
  • Threads and thread sanitizer

Version history

Second Edition · iOS 13, Swift 5.1, Xcode 11

v2.0 · Nov 25 2019 · Scott Grosch
Pro

Concurrency by Tutorials

By Scott Grosch

The book that teaches you everything there is to know about how to write performant and concurrent code for your iOS apps.

Read for Free with an Ultimate Pro Subscription* * Includes this and all other books in our online library See all benefits
Buy Individually $59.99 $29.99* *Includes access to all of our online reading features.
See buying options
Hide buying options

Spring Ahead Sale —
Save on Everything.
All videos. All books. Now 50% off.

Build your mobile development skills and save! The mobile development world moves quickly — make 2021 the year you stand out from the rest. Ultimate book & video subscriptions start at just $149/year for as part of our Spring Ahead sale.