SwiftUI Academy · บทเรียน

ตัวปรับแต่ง .task

โหลดข้อมูลเมื่อมุมมองปรากฏ

บทเรียน 3 จาก 413 ขั้นตอน

ตัวปรับแต่ง .task เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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. 👍

เริ่มต้นได้ฟรี

เรียนรู้ Swift ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “ตัวปรับแต่ง .task” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ตัวปรับแต่ง .task” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัวปรับแต่ง .task”

โหลดข้อมูลเมื่อมุมมองปรากฏ คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “ตัวปรับแต่ง .task” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ถอดรหัส JSON ด้วย Codable
  2. เรียกใช้ API ด้วย URLSession
  3. ตัวปรับแต่ง .task
  4. สถานะกำลังโหลดและข้อผิดพลาด
← กลับไปที่ SwiftUI Academy