Contents
Introduction 6
This stuff is hard
How to follow this guide
Concurrency vs parallelism
Understanding threads and queues
Main thread and main queue: what’s the difference?
Where is Swift concurrency supported?
Dedication
Async/await 17
What is a synchronous function?
What is an asynchronous function?
How to create and call an async function
How to call async throwing functions
What calls the rst async function?
What’s the performance cost of calling an async function?
How to create and use async properties
How to call an async function using async let
Sending data safely across actor boundaries
What’s the difference between await and async let?
Why can’t we call async functions using async var?
How to use continuations to convert completion handlers into async functions
How to create continuations that can throw errors
How to store continuations to be resumed later
How to x the error “async call in a function that does not support concurrency”
Sequences and streams 68
What’s the difference between Sequence, AsyncSequence, and AsyncStream?
How to loop over an AsyncSequence using for await
How to manipulate an AsyncSequence using map(), lter(), and more
How to create a custom AsyncSequence
How to convert an AsyncSequence into a Sequence
How to create and use AsyncStreams to return buffered data
Tasks and task groups 97
What are tasks and task groups?
www.hackingwithswift.com 3
fi
fi
fi
How to create and run a task
What’s the difference between a task and a detached task?
How to make a task sleep
How to get a Result from a task
How to control the priority of a task
Understanding how priority escalation works
How to cancel a Task
How to voluntarily suspend a task
How to create a task group and add tasks to it
How to cancel a task group
How to handle different result types in a task group
How to discard results in a task group
What’s the difference between async let, tasks, and task groups?
How to make async command-line tools and scripts
How to create and use task local values
How to run tasks using SwiftUI’s task() modi er
Is it ef cient to create many tasks?
Actors 170
What is an actor and why does Swift have them?
How to create and use an actor in Swift
Understanding actor initialization
How to make function parameters isolated
How to make parts of an actor nonisolated
How to use @MainActor to run code on the main queue
Who decides which actor code runs on?
Understanding how global actor inference works
What is actor hopping and how can it cause problems?
What is actor reentrancy and how can it cause problems?
What’s the difference between actors, classes, and structs?
Important: Do not use an actor for your SwiftUI data models
Testing 210
Introduction to testing Swift concurrency
How to write basic async tests
How to handle concurrency errors in unit tests
How to test completion handlers with Swift Testing and XCTest
How to test `AsyncSequence` and `AsyncStream`
How to set a time limit for concurrent tests
How to force concurrent tests to run on a speci c actor
How to serialize parameterized tests with Swift Testing
4 www.hackingwithswift.com
fi
fi
fi
Solutions 231
How to download JSON from the internet and decode it into any Codable type
Afterword 236
www.hackingwithswift.com 5