ดึงเพื่อรีเฟรช
โหลดข้อมูลใหม่ด้วยตัวปรับแต่ง refreshable
ดึงเพื่อรีเฟรช เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Refreshing on Demand
Users expect to pull a list down to reload it. SwiftUI bakes this gesture in with a single modifier, no gesture code required. ✨
Meet refreshable
Attach the refreshable modifier to a List. Pulling down reveals a spinner and runs the async work you provide.
.refreshable {
await reload()
}It Expects Async Work
The refreshable closure is async, so it pairs perfectly with await and modern Swift concurrency for fetching data.
Spinner Tied to Your Task
The pull-to-refresh spinner stays visible until your await finishes. SwiftUI hides it the moment the work completes.
A Simple Reload
Inside refreshable, call an async function that fetches fresh data and updates your @State array on completion.
func reload() async {
items = await fetchItems()
}Awaiting a Network Call
A real reload usually awaits a network request. Use URLSession with async/await to pull new items from a server.
let (data, _) = try await
URLSession.shared.data(from: url)Update State After Await
Once the await returns, assign the decoded results to your @State array and the list refreshes with the new rows.
let fresh = try decoder.decode(
[Item].self, from: data)
items = freshHandle Errors Gracefully
Network calls can fail. Wrap the work in do/catch so a dropped connection shows a message instead of crashing.
do {
items = try await fetchItems()
} catch {
showError = true
}Refreshable Needs a List
The pull gesture only works on scrollable containers like List or ScrollView. Attaching refreshable to a VStack does nothing.
Keep It Quick
Refresh should feel snappy. If your fetch is slow, show progress and avoid blocking the main thread with heavy work.
Pairs With Other Editing
Pull-to-refresh sits happily beside onDelete, onMove, and add. Together they make a fully interactive, editable list. ✨
Quick Check
What kind of closure does refreshable take?
Recap: Pull-to-Refresh
You added reload-on-pull with the async refreshable modifier on a List, awaiting fresh data and handling errors. 🎉
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “ดึงเพื่อรีเฟรช” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ดึงเพื่อรีเฟรช” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ดึงเพื่อรีเฟรช”
โหลดข้อมูลใหม่ด้วยตัวปรับแต่ง refreshable คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ดึงเพื่อรีเฟรช” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ