ดึงข้อมูลด้วย @FetchRequest
โหลดออบเจ็กต์ที่จัดการแล้วเข้าสู่มุมมองโดยตรง
ดึงข้อมูลด้วย @FetchRequest เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Reading Records in Views
To show saved data, a SwiftUI view needs to fetch it from Core Data and re-render whenever that data changes.
The @FetchRequest Wrapper
The @FetchRequest property wrapper runs a query for an entity and keeps the results live and current for you.
@FetchRequest(sortDescriptors: []) var tasks: FetchedResults<Task>What You Get Back
Results arrive as FetchedResults, a collection you can loop over just like an array inside your view body.
Showing Them in a List
Drop the results straight into a List with ForEach to render one row per stored record.
List(tasks) { task in
Text(task.title ?? "Untitled")
}Sorting the Results
Pass sortDescriptors so rows arrive in a predictable order instead of a random one.
@FetchRequest(sortDescriptors: [SortDescriptor(\.title)]) var tasks: FetchedResults<Task>Filtering with a Predicate
Add an NSPredicate to fetch only the records that match a rule, like done items or a search term.
@FetchRequest(sortDescriptors: [], predicate: NSPredicate(format: "isDone == true")) var done: FetchedResults<Task>It Stays Live
@FetchRequest watches the context, so when you add or delete a record the view updates automatically. ⚡
It Needs the Context
@FetchRequest reads the context from the environment, so the value you injected in the stack must be present.
Handling an Empty Result
When the fetch returns nothing, show a friendly empty state instead of a blank screen.
if tasks.isEmpty {
Text("No tasks yet")
}Optional Attributes
Core Data attributes are often optional in Swift, so unwrap them safely before you display their values.
Declare It as var
Declare a @FetchRequest property as var, since the wrapper updates its stored results as the data changes.
Quick Check
You wired up @FetchRequest. A quick check on how it behaves.
Recap
You used @FetchRequest to pull records into a view, sort and filter them, and let the list refresh itself as data changes. ✨
เรียนรู้ Swift ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 30
- บทเรียน
- 120
คำถามที่พบบ่อย
บทเรียน “ดึงข้อมูลด้วย @FetchRequest” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ดึงข้อมูลด้วย @FetchRequest” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ดึงข้อมูลด้วย @FetchRequest”
โหลดออบเจ็กต์ที่จัดการแล้วเข้าสู่มุมมองโดยตรง คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “ดึงข้อมูลด้วย @FetchRequest” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม
ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- สแตก Core Data
- ดึงข้อมูลด้วย @FetchRequest
- สร้างและบันทึกเอนทิตี
- เพรดิเคตและตัวบอกการเรียงลำดับ