What is Swift concurrency? Why is it better than GCD? What about Combine?
In this course, you’ll learn how to create safe, performant and predictable
asynchronous apps using these Swift concurrency features: async/await, Tasks,
asynchronous sequences and MainActor.
This content was released on Oct 18 2022. The official support period is 6-months
from this date.
What is Swift concurrency? Why is it better than GCD? What about Combine?
In this course, you’ll learn how to create safe, performant and predictable
asynchronous apps using these Swift concurrency features: async/await, Tasks,
asynchronous sequences and MainActor.
Cinema mode
Mark complete
Download course materials
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
00:02Hi, I’m Audrey. Welcome to our course Modern Concurrency: Getting Started, about the new Swift concurrency model. In this video, you’ll find out what’s in store for you as you work through this course.
00:15Here’s some code that should look familiar.
00:17It calls a network API and assigns the result to a property on your view model. There are lots of potential coding errors: Did you forget to check for an error? Did you really call the completion closure in every code path?
00:31Grand Central Dispatch was originally designed for Objective-C so, when Swift came along, Apple couldn’t integrate asynchrony tightly into the new language design.
00:41The compiler has no way of knowing how many times you’ll call completion inside fetchServerStatus(), so it can’t optimize its lifespan and memory usage.
00:51You need to handle memory management yourself by weakly capturing viewModel, then checking in the code to see if it was released before the closure runs.
01:01The compiler has no way to make sure you handled the error. In fact, if you forget to handle error in the closure, or don’t invoke completion at all, the method will just freeze without warning.
01:13The Swift concurrency model provides new tools to achieve the same goals as the GCD example:
01:22async indicates that a method or function is asynchronous. Using it lets you suspend execution until an asynchronous method returns a result.
01:32await indicates that your code might pause execution while it waits for an async-annotated method or function to return.
01:42A Task is a unit of asynchronous work. You can wait for a task to complete or cancel it before it finishes.
01:49This Swift concurrency code has fewer lines than the GCD example, but it provides a lot more information to the compiler and the runtime:
01:59This async keyword tells the compiler that fetchServerStatus() is an asynchronous function that can suspend and resume execution one or more times in the future.
02:11fetchServerStatus() either returns ServerStatus data or throws an error. The compiler checks this, so you don’t have to worry about forgetting to handle an erroneous code path!
02:23Task executes its code in an asynchronous context, so the compiler knows what code is safe (or unsafe) to write in this closure. It won’t let you write potentially unsafe code, like mutating shared state.
02:39You call an asynchronous function with the await keyword. This gives the runtime a chance to suspend or cancel your code and lets the system constantly update the priorities in the current task queue. The runtime handles this for you, so you don’t have to worry about threads and cores.
02:59The new Swift concurrency model is tightly integrated with the language syntax, the Swift runtime and Xcode.
In addition to async/await, you also get:
03:09Context-aware code compilation: The compiler keeps track of whether a given piece of code could run asynchronously. This enables new features like actors, which differentiate between synchronous and asynchronous access to their state at compile time and protect against corrupting data by making it harder to write unsafe code.
03:33Structured concurrency: Each asynchronous task is now part of a hierarchy, with a parent task and a given priority of execution. This hierarchy allows the runtime to cancel all child tasks when a parent is canceled or wait for all children to complete before the parent completes. It’s all under control. Outcomes are more obvious, with high-priority tasks running before any low-priority tasks in the hierarchy.
04:02And a cooperative thread pool: The new model manages a pool of threads to make sure it doesn’t exceed the number of CPU cores available. This way, the runtime doesn’t need to create and destroy threads or constantly perform expensive thread switching. Instead, your code can suspend and, later on, resume very quickly on any of the available threads in the pool.
04:57What About Combine?
04:29Combine is a higher level, more complex framework, based on the reactive streams protocol. Its operators help you combine events and data over time from various sources, enabling you to solve very complex problems.
04:46Swift concurrency offers lower-level APIs for executing asynchronous code. Use it to replace GCD and Operations with tasks and async sequences and save Combine for more complex apps.
05:01It’s easy to integrate Combine into Swift concurrency, looping asynchronously over Publisher.values. You’ll do this in Part 2 of this course.
05:11And Swift concurrency APIs are available on Linux while Combine isn’t.
05:17In this course, you’ll learn how to use fundamental concepts like Task, MainActor, async/await, async let and AsyncSequence. And you’ll practice different ways to cancel tasks.
05:30The next course, Modern Concurrency: Beyond the Basics covers AsyncStream, Continuations, Unit Testing, TaskGroup, Actors and thread-safety.
05:41You’ll implement asynchronous and concurrent code in real iOS apps. And the course materials include a server to respond to requests from these apps.
05:53Your first app is a stock ticker app. First, you’ll fetch a list of stock symbols, then you’ll fetch and display a never-ending sequence of stock prices.
06:03In Part 2, you’ll work on SuperStorage, a cloud file storage app with three download plans.
06:11Here’s what you need to work through these courses:
06:13A Mac running macOS Monterey (12.0) or later. Big Sur should work, but this course uses macOS Monterey, so your mileage may vary.
06:25Xcode 13: Since Xcode 13.2, all the new Swift concurrency features work with iOS 13 / macOS 10.15 SDK (or later). If you use an older version of Xcode 13, you’ll only get Swift concurrency support when targeting iOS 15 or macOS 12.
06:49An intermediate level of Swift: You’ll appreciate Swift concurrency more if you’re familiar with Grand Central Dispatch and URLSession. And you’ll be doing a lot of throwing and do-try-catching errors.
07:03SwiftUI: The sample projects have all the views and navigation code in place, but you should have some familiarity with SwiftUI. You’ll need to add a few view modifiers and actions to call your asynchronous methods from SwiftUI views.
07:20This course is based on the book by the amazing Marin Todorov. He’s icanzilb on Twitter.
This video series has been tech edited by Adrian Strahan. Find him on twitter as adrianstrahan.
07:35In the next episode, you’ll explore some asynchronous code in a playground. Then you’ll use what you learn in your first asynchronous app.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.