Transcript: 01. Introduction
Hi, 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. Here's some code that should look familiar. It 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? Grand Central Dispatch was originally designed for Objective-C, so when Swift came along, Apple couldn't integrate a synchrony tightly into the new language design. That compiler has no way of knowing how many times you'll call completion inside fetch service status, so it can't optimize its lifespan and memory usage. You need to handle memory management yourself by weekly capturing view model and then checking in the code to see if it was released before the closure runs. The 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. The Swift Concurrency model provides new tools to achieve the same goals as the GCD example. Async indicates that a method or function is asynchronous. Using it lets you suspend execution until the asynchronous method returns the result. Await indicates that your code might pause execution while it waits for an async annotated method or function to return. A Task is a unit of asynchronous work. You can wait for a Task to complete or cancel it before it finishes. This Swift Concurrency code has fewer lines than the GCD example, but it provides a lot more information to the compiler and the runtime. This async keyword tells the compiler that Fetch service status is an asynchronous function that can suspend and resume execution one or more times in the future. Fetch service status either returns service status data or throws an error. The compiler checks this so you don't have to worry about forgetting to handle an erroneous code path. Task 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. You 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. The new Swift Concurrency model is tightly integrated with the language syntax, the Swift runtime, and Xcode. In addition to async/await, you also get Context-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. Structured 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. And 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. And what about Combined? Combined is a higher level, more complex framework, based on the reactive streams protocol. It's operators help you combine events and data over time from various sources, enabling you to solve very complex problems. Swift 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. It's easy to integrate Combine into Swift concurrency, looping asynchronously over publisher.values. You'll do this in part two of this course. And Swift concurrency APIs are available on Linux while Combine isn't. In this course, you'll learn how to use fundamental concepts like Task, MainActor, async await, async let, and async sequence. And you'll practice different ways to cancel tasks. The next course, Modern Concurrency Beyond the Basics, covers AsyncStream, Continuations, Unit Testing, TaskGroup, Actors and thread safety. You'll implement asynchronous and concurrent code in real iOS apps. And the course materials include a server to respond to requests from these apps. Your first app is a stock ticker app. First, you'll fetch a list of stock symbols and then you'll fetch and display a never-ending sequence of stock prices. In part two, you'll work on SuperStorage, a cloud file storage app with three download plans. Here's what you need to work through these courses. A Mac, running MacOS Monterey 12.0 or later. Big Sir should work, but this course uses MacOS Monterey so your mileage may vary. Xcode 13.2 or later since Xcode 13.2, all the new Swift Concurrency features work with iOS 13 or MacOS 10.15 SDKs or later. If you use an older version of Xcode 13, you'll only get Swift Concurrency support when targeting iOS 15 and MacOS 12. You need an intermediate level of Swift. You'll 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. SwiftUI. The sample projects have all the views and navigation code in place but you should have some familiarity with SwiftUI. You will need to add a few view modifiers and actions to call your asynchronous methods from SwiftUI views. 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 some async code in a playground and then you'll use what you learn in your first asynchronous app. Let's get started.