The .task Modifier
Load data when a view appears.
The .task Modifier is a free SwiftUI Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Loading When a View Appears
Often you want to fetch data the moment a screen shows. The .task modifier runs async work exactly when the view appears. ⏱️
Attaching .task
Add .task to any view and give it an async closure. SwiftUI starts that work as the view comes on screen.
List(users) { user in Text(user.name) }
.task { await load() }It Calls Async Code
Inside .task you can freely use await. That makes it the natural home for your URLSession fetch on appearance.
.task {
users = try? await loadUsers()
}Why Not onAppear
The older onAppear cannot await async work directly. The .task modifier is built for concurrency, so prefer it for loading.
Automatic Cancellation
When the view disappears, SwiftUI cancels the task for you. That stops wasted work and avoids updating a gone view. ✨
Reacting to an id
Pass an id to .task and it reruns whenever that value changes. Perfect for reloading when a selection updates.
.task(id: selectedID) {
await loadDetail(selectedID)
}Updating State After Fetch
Store results in @State so assigning them refreshes the UI. The view redraws as soon as the data arrives.
@State private var users: [User] = []Handling Errors Inside
Wrap risky calls in do/catch inside the task. You can then set an error message that the view shows the user.
do { users = try await loadUsers() }
catch { errorText = "Failed" }Runs on the Main Actor
A .task closure starts on the main actor, so updating state and UI from it is safe without extra hopping.
One Task per Lifetime
By default .task runs once per appearance. For repeated refresh use pull-to-refresh or a changing id instead.
Keeping the Closure Small
Call a named function from .task rather than stuffing logic inline. It keeps the view tidy and easy to read.
.task { await viewModel.refresh() }Quick Check
Quick check on triggering async loads.
Recap: .task
You learned that .task runs async work when a view appears, cancels on disappear, can rerun on an id, and is the go-to place to fetch. 👍
Frequently asked questions
Is the “The .task Modifier” lesson free?
Yes — the full text of “The .task Modifier” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “The .task Modifier”?
Load data when a view appears. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The .task Modifier” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.