I am the author of Core Data Fundamentals with Swift, CloudKit, iOS Data Persistence: The Big Picture, and eight other courses on Pluralsight.
Deepen your understanding by watching the courses!
How to Use @FetchRequest in SwiftUI
Once you’ve passed an NSManagedObjectContext instance to your SwiftUI View, you’ll need to pull data out of your Core Data persistent store with a fetch request.
First Things First
- The remainder of this how-to assumes that you’ve already got a way to initialize the Core Data stack. Ticking the ‘Use Core Data’ checkbox when you start a new app will place some boilerplate code in
AppDelegate
. @Environment(\.managedobjectcontext)
must be assigned before the View is initialized for@FetchRequest
to work.- Code examples are taken from my “Blog Idea List” sample project that includes a single Core Data Entity named
BlogIdea
:
You can refer to my introduction to using Core Data with SwiftUI to review all of the steps in one spot. It includes an example project with all of the pieces stitched together!
Breaking Down the Code
There are three key components to this:
- A way to configure an
NSFetchRequest
with anNSSortDescriptor
added. Your app will blow up 💥 if you don’t have one added to your fetch request. - The
@FetchRequest
property wrapper in your SwiftUI view. - A SwiftUI View that displays the results of the executed fetch request.
The following code snippets provide you an example of how to weave all three components together.
Comments with ❇️ symbols will explain the details inline.
BlogIdea.swift
|
|
ContentView.swift
|
|
Note that you don’t actually need to hold a reference to the @Environment
’s managedObjectContext
.
@Environment
’s managedObjectContext
must be set! But you don’t have to “call” it or use it directly to make the fetch request “go”.
Where’s the Call to Perform the Fetch Request?
With most Core Data apps, you’re going to see a call to fetch(_:)
somewhere in your code.
This happens “behind the scenes” when you use the @FetchRequest
property wrapper in your SwiftUI view. No need to use your managed object context to perform the fetch request manually.
For a more complete guide to using Core Data with SwiftUI, you can refer to the introduction I published. It includes an example project with all of the pieces stitched together!