Modern Concurrency: Beyond the Basics
Oct 20 2022 · Swift 5.5, iOS 15, Xcode 13.4
Part 1: AsyncStream & Continuations
About this episode Leave a rating/review See forum comments Cinema mode Mark complete Download course materials Next episode: 02. AsyncStream
Notes: 01. Introduction
- Prerequisites: Familiarity with the topics in Modern Concurrency: Getting Started. Intermediate level knowledge of Swift, URLSession, Grand Central Dispatch and error handling. Some familiarity with SwiftUI is helpful.
- Modern Concurrency book
- iOS Concurrency with GCD and Operations video course
- Concurrency by Tutorials book
Transcript: 01. Introduction
Hi, I'm Audrey. Welcome to our course, "Modern Concurrency: Beyond the Basics." In this video, you'll find out what's in store for you as you work through this course. This course follows on from, "Modern Concurrency: Getting Started" and assumes you're familiar with what that course covers. You should be comfortable calling and writing asynchronous functions and iterating over asynchronous sequences. In part one of this course, you'll learn about AsyncStream, unit testing, and continuations. Part two introduces TaskGroup for truly dynamic concurrency and goes beyond main actor to show you how to use the actor API to make your code thread safe. Now, I'll tell you a little more about the topics in part one. Whenever Apple introduces something new, like Swift or SwiftUI, they don't expect developers to stop using the old way and switch over completely to the new. There are always bridging tools to make it easier to integrate your existing code with the new stuff. AsyncStream is one of these tools. You can use it to create async sequences from the completion handlers and delicate methods of existing asynchronous APIs. You'll also learn how to use AsyncStream to create custom asynchronous sequences without adding new types to your app. You just specify the element type and then supply a trailing closure to provide the sequence. There are two types of closure. You'll use one type to create countdown sequence and the other type to stream notifications. Since Apple first introduced Grand Central Dispatch, it has advised developers on how to avoid the dangers of threat explosion. In the Swift Concurrency system, there are at most only as many threads as there are CPU course. When threads execute work under Swift Concurrency, the system uses a continuation. A lightweight object to keep track of where to resume work on a suspended task. When a task suspends, it captures its state in a continuation. Its thread can resume execution of another task, recreating its state from the continuation it created when it's suspended. The cost of this is a function call. Switching between task continuations is much cheaper and more efficient than performing context switches. This all happens behind the scenes when you use async functions, but you can also get your hands on a continuation to manually resume execution. The buffering form of AsyncStream uses a continuation to yield stream elements. And you'll use a manual continuation API to help you convert existing code, like completion handlers and delegate methods into async functions. Before Swift Concurrency, XCTest didn't have language support for running asynchronous code. You had to rely on workarounds, like XCTWaiter and expectations. With Swift Concurrency, you just declare any test method as async. When you await an asynchronous function, the test suspends. When it resumes, you can verify the output as usual. You'll work through a simple test case with a single await and a more complex one that captures test output over time. You will also create some tools to create tasks that time out instead of hanging and also speed up task duration. The app you'll work on in part one is a messaging app called Blabber. The Starter Project already implements the basic chat view. You'll implement a countdown timer, using both AsyncStream closure types. And then you'll use the buffered AsyncStream to add notifications when a user leaves or returns. You'll use continuations with Core Location to let users show their location. And you'll learn how easy it is to write unit tests for Blabber's networking code. Here's what you need to work through these courses. A Mac running macOS Monterey 12.0 or later. Big Sur should work, but this course uses macOS Monterey. So your mileage may vary. Xcode 13. Since Xcode 13.2, all the new Swift Concurrency features work with iOS 13 and macOS 10.15s SDK or later. If you use an older version of Xcode 13, you will only get Swift Concurrency support when targeting iOS 15 or macOS 12. You need an intermittent level of Swift. You will appreciate Swift Concurrency more if you're familiar with the Grand Central Dispatch, and URLSession. And you'll be doing a lot of throwing and do try catching errors. You need SwiftUI. The sample projects have all the views and navigation code in place, but you should have some familiarity with SwiftUI. You need to add a few view modifiers and actions to call your asynchronous methods from SwiftUI views. And you need the basics of async await and async sequence, equivalent to the getting started course. This 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 Adrian Strahan. In the next episode, you'll explore AsyncStream in a playground. And then you'll use what you learned in the Blabber app. Let's get started.