Adding Core Data objects from a SwiftUI view
A storage without any data in it is useless. In this recipe, we will learn how easy it is to implement a function that will add data to Core Data from a SwiftUI view.
In this recipe, we are going to implement a simple Contacts app where we can add storable contact profiles to Core Data for persistent storage.
Getting ready
Create a SwiftUI app called AddContacts
.
Before you start this recipe, make sure you have completed steps 1 to 6 of the Showing Core Data objects with @FetchRequest recipe. Then, you can work on this recipe.
How to do it…
Besides adding profiles, we must present our list of already saved contacts. For this, we are going to reuse some of the code from the Showing Core Data objects with @FetchRequest recipe.
To add a contact, we are going to implement a simple modal view, presented as a sheet, with three text fields: two for the name, and one for the phone number. Let’s get started...