0Pricing
SwiftUI Academy · Lektion

Der .task-Modifikator

Laden Sie Daten, wenn eine Ansicht erscheint.

Der .task-Modifikator ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 👍

Häufig gestellte Fragen

Ist die Lektion „Der .task-Modifikator“ kostenlos?

Ja — der vollständige Text von „Der .task-Modifikator“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Der .task-Modifikator“?

Laden Sie Daten, wenn eine Ansicht erscheint. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um SwiftUI Academy zu starten?

Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Der .task-Modifikator“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?

Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. JSON mit Codable dekodieren
  2. APIs mit URLSession aufrufen
  3. Der .task-Modifikator
  4. Lade- und Fehlerzustände
← Zurück zu SwiftUI Academy