Integrating Core Data with SwiftUI
- Eric Palma
- Apr 9
- 6 min read
Updated: 3 days ago
A key skill every iOS developer should have is the ability to implement a robust data persistence strategy. Data persistence in iOS is the ability for data to outlive the app that created it.
In this post I will demonstrate how to devise a robust strategy for persisting data and how to integrate it into your SwiftUI apps using CoreData.
What is CoreData and why use it?
Core Data is Apple’s native framework that simplifies the task of persisting data in an object oriented manner. This means that the framework is not a database, contrary to what most beginners might initially believe, rather an abstraction built on top of a data store with additional capabilities that help us manage our data.
As a result, the framework is a powerful tool that let's us focus on our app's unique experience rather than worrying about data storing tasks.

How to use CoreData in SwiftUI apps
Throughout this guide, I want you to remember the following: Core Data is an object graph and persistence framework for iOS, macOS, watchOS, and tvOS. It provides an abstraction layer for data storage and management, often using SQLite as the underlying storage, and is tightly integrated with Xcode.
If you remember the above, you will have an easier time understanding why there are certain steps you must take in order to use CoreData.
Here are the high-level required steps to integrate Core Data into your app:
Define a model file.
Define entities.
Setup the Core Data stack.
Inject the Core Data stack into the SwiftUI hierarchy
The Core Data model file and entities
As the name implies, the Core Data model file is a representation of the objects you wish to use within the Core Data framework. It holds the definition of entities and both their respective properties and relationships to any other entities.
This is how we as iOS developers tell the Core Data framework what data we want it to help us manage and persist.
To create the model file:
In Xcode, go to File > New > File from template > Data Model
Name your Data Model file (I will use NoteApp) and click Create
Once you are done with the above steps, you will see a file with the extension .xcdatamodeld . You can open that file and begin defining your entities - but before we add any entities to our model file, it’s good practice to diagram them along with any relationships to other entities. This will make it easier to reason about our app's data.
To walk through an example, imagine that we are building a note taking app. All this app will do is allow users to create and save notes. For our purposes we will keep the example simple and only focus on persisting Note data. Below is a UML diagram of the entity we will be persisting with Core Data.

Now we are ready to define the entity in our Core Data model file so that we can use the framework to store and fetch it. In Xcode, we will select the xcdatamodeld file to open it in the editor window. We are now ready to add our entity and define its properties.
To begin, we have to open the model file in Xcode, then we click on Add Entity. Next we name the entity, Note in this case. Lastly, we define the entities attributes. Looking at the UML for the Note entity above, we would define the attributes as follows:

Core Data stack for SwiftUI apps
Want to read more?
Subscribe to curiousalgorithm.com to keep reading this exclusive post.