Deleting Core Data objects from a SwiftUI view
How can you delete objects from a Core Data repository? Removing objects is almost as important as adding them. In this recipe, we’ll learn how to integrate the Core Data delete options into a SwiftUI app.
Getting ready
Create a SwiftUI app called DeleteContacts
.
Before we start this recipe, make sure you have completed steps 1 to 10 of the Showing Core Data objects with @FetchRequest recipe. Then, you can complete this recipe.
How to do it…
We are going to reuse part of the code provided in the Showing Core Data objects with @FetchRequest recipe. Please refer to that recipe if you want to find out more.
Let’s get started:
- Switch to the
App
struct and inject the instance ofCoreDataStack
into theEnvironment
. After this change, thebody
variable should be as follows:var body: some Scene { WindowGroup { ContentView() .environment(\.managedObjectContext...