Integrating Core Data with SwiftUI
- Eric Palma
- Apr 9
- 5 min read
Updated: Apr 29
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, we will learn what Core Data is and then I’ll show you how to use it to manage and persist your app’s data, and how to seamlessly integrate it into your SwiftUI apps.
What is CoreData and why use it?
Core Data is Apple’s object graph and persistence framework for iOS, macOS, watchOS, and tvOS. It provides an abstraction layer for managing and storing data—often using SQLite under the hood—and is tightly integrated with Xcode, allowing you to focus on your app’s logic while it handles data storage, relationships, and lifecycle management.

If you remember the above, you will have an easier time understanding why there are certain steps you must take in order to use Core Data with SwiftUI.
How to integrate Core Data with SwiftUI
Here are the high-level required steps to setup and integrate Core Data into your app:
Define a model file.
Define your entities.
Setup the Core Data stack.
Inject the Core Data stack into your SwiftUI hierarchy
Saving data with Core Data would be part of the data layer when building an app using Clean Architecture.
The Core Data model file and entities
As the name suggests, the Core Data model file acts as a blueprint for your app’s data structure. It holds definitions for entities, their properties, and the relationships between 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 a basic example, imagine that we are building a note taking app. All this app will do is 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.
By clicking on Add Entity we will have the opportunity to create and name a new entity ( we will name our new entity Note). 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
With the Note entity defined within our model file, the next step is to set up our code stack for working with the Note entity within the Core Data framework. Remember when I mentioned earlier that Core Data is an abstraction for data management and persistence? The consequence of working with abstractions is that there is typically some work needed to apply the abstraction to concrete use cases.
Our concrete use case here is that we want to persist Note data. So far we’ve told the framework about our model, but that is only half of the journey. The next crucial step is initializing the Core Data objects that our app’s code will interact with.
The following objects need to be configured and initialized:
NSManagedObjectModel - represents our app’s model file in code.
NSManagedObjectContext - tracks changes to instances of our app’s managed objects.
NSPersistentStoreCoordinator - does the actual saving and fetching of our app’s managed objects from a designated store.
NSPersistentContainer - assists us in setting up all the above at once.
Want to read more?
Subscribe to curiousalgorithm.com to keep reading this exclusive post.