Modern Concurrency in Swift |

Master Swift’s new concurrency model!

For years, writing powerful and safe concurrent apps with Swift could easily turn into a daunting task, full of race conditions and unexplained crashes hidden in a massive nesting of callback closures.

In Swift 5.5, Apple introduced a new concurrency model featuring the async/await syntax, to let you write asynchronous code that reads like synchronous code. But like any new feature, here be dragons! So how will you achieve the much-desired mastery of Modern Swift Concurrency?

Modern Concurrency in Swift is here for the rescue, showcasing everything you need to know about async/await, tasks, actors, and everything in between!

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.

Section I: Modern Concurrency in Swift

Get to know the modern Swift concurrency model, then take a deep dive into async code by working on a real-time stock trading project.

Go into more detail about how the async/await syntax and the cooperative asynchronous execution work. Additionally, it introduces the usage of “async let” to design concurrent code and the “Task” type which encapsulates asynchronous execution in the modern concurrency model.

Work with async sequences in more detail, proxying Combine to async/await code and managing async tasks.

Create custom asynchronous sequences by leveraging AsyncStream, which lets you wrap existing asynchronous work in your app as an asynchronous sequence without breaking a sweat.

Learn about continuations, which are a way to manually manage suspended functions. You’ll use them to bridge between existing non-async/await code and modern, concurrent APIs.

Develop an asynchronous testing infrastructure and learn all the ins-and-outs of testing asynchronous code in the new concurrency model. You’ll wrap up by adding unit tests to the Blabber project you worked on in previous chapters.

Create dynamic concurrency with Task Groups, which let you run an arbitrary number of tasks concurrently and offers a natural sequence interface to access and iterate over the results of those tasks.

Actor is the new Swift type that transparently protects its mutable state from data races in a synchronized container, making concurrent programming much safer.

Now that you know actors, you’ll learn about a special subset of them — global actors, which allow easy and safe access to shared resources from anywhere in your app.

Create a distributed system that uses actors to execute concurrent tasks on multiple devices over the local network.

Leave a Comment