Swift Task

Task is the unit of concurrent work in Swift's structured concurrency model. Creating a Task starts an async job right away, and Swift tracks its priority, cancellation, and lifetime automatically instead of leaving you to manage threads by hand.
Why it exists
Before Swift concurrency, asynchronous code relied on completion handlers or GCD's DispatchQueue, which do not compose well: cancellation, error propagation, and priority had to be threaded through manually, and it was easy to leak work or call a completion handler twice. Task exists as the building block of structured concurrency, giving every unit of async work a defined lifetime, a parent, and automatic cancellation propagation.
The mental model
Think of a Task as a receipt for an order handed to a kitchen. You do not manage the cooks or the stove, that is the cooperative thread pool's job, you just hold the receipt, which lets you check if the order is ready, cancel it, or await the result. Structured tasks, created inside async let or a task group, are receipts your parent order refuses to close out until they are done.
How it works
Task { } launches unstructured work immediately on Swift's cooperative thread pool, inheriting the current actor and priority unless told otherwise, and returns a handle you can await or cancel. Cancellation is cooperative: the task is only ever marked cancelled, its code must check Task.isCancelled or call Task.checkCancellation to actually stop. Structured alternatives, async let for a fixed number of children and a task group for a dynamic number, automatically cancel their children if the parent task is cancelled or throws, and the parent scope cannot exit until every child finishes, which is what makes them structured rather than fire and forget.
When it matters
It matters anywhere concurrent work needs a clear owner: a Task launched from a SwiftUI view's onAppear must be cancelled when the view disappears, or it keeps running and can crash by touching a deallocated view model. The footgun is treating cancellation as automatic: a plain Task { } does not stop just because the caller lost interest, the task body must actively check for cancellation, especially inside loops doing network or file work.
A concrete example
A SwiftUI screen starts a search request inside .task { await viewModel.search(query) }, a modifier that creates a Task tied to the view's lifetime and cancels it automatically when the view disappears. Inside search, a loop over paginated results calls try Task.checkCancellation() each iteration, so navigating away mid search stops the network calls instead of wasting bandwidth updating a view that no longer exists.
Interview question
A developer starts a network call with Task { await viewModel.load() } inside a button action, then the user immediately navigates away. What actually stops the task?
- a.The task pauses itself once its priority drops below the app's active priority level
- b.Swift automatically cancels any Task whose creating view is deallocated
- c.Nothing automatically, unstructured Task work keeps running unless something explicitly cancels the handle or checks for cancellationCorrect
- d.ARC deallocates the Task object along with the button, which halts its body immediately
Why? this is the answer
A Task created with Task { } is unstructured and has no automatic tie to a view's lifecycle, so it keeps running until it finishes, throws, or something calls cancel on its handle and the body checks for that cancellation. SwiftUI's .task modifier provides automatic cancellation, but a plain Task { } inside a button action does not.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles