Fetching with @FetchRequest
Load managed objects directly into views.
Fetching with @FetchRequest is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. ✨
Frequently asked questions
Is the “Fetching with @FetchRequest” lesson free?
Yes — the full text of “Fetching with @FetchRequest” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Fetching with @FetchRequest”?
Load managed objects directly into views. You practise SwiftUI Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Fetching with @FetchRequest” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this SwiftUI Academy lesson?
Yes. Every SwiftUI Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The Core Data Stack
- Fetching with @FetchRequest
- Creating & Saving Entities
- Predicates & Sort Descriptors