.taskモディファイア
ビューの表示時にデータを読み込みます。
「.taskモディファイア」はCoddyKit上の無料SwiftUI Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSwiftUI Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 SwiftUI Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. 👍
よくある質問
「.taskモディファイア」レッスンは無料ですか?
はい。「.taskモディファイア」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、SwiftUI Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 SwiftUI Academyコースには全4レッスンが含まれています。
「.taskモディファイア」で何を学びますか?
ビューの表示時にデータを読み込みます。 ブラウザで直接実行するハンズオンコードでSwiftUI Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
SwiftUI Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSwiftUI Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「.taskモディファイア」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSwiftUI Academyレッスンでコードを書いて実行できますか?
はい。すべてのSwiftUI Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。