You are reading this tutorial, which means you are interested in learning more about the Core Data framework. It probably also means that you have heard a word or two about the Core Data stack. But what is it? And what is its role in a Core Data application? In this tutorial, you learn everything you need to know about the Core Data stack.
Don’t Skip This
The most intimidating concept for developers new to Core Data is the Core Data stack. But that is fine. I promise you that, at the end of this tutorial, you understand the ins and outs of the Core Data stack. That is one checkbox you can tick.
Unfortunately, many developers don’t take the time to learn about the Core Data stack. They either give up or continue struggling with the Core Data framework. Don’t make this mistake. By reading this tutorial, you give yourself a head start.
Three Objects
Before we dissect the Core Data stack, you need to know three things. First, every Core Data application has a Core Data stack. Second, a Core Data application is useless without a Core Data stack. Third, a Core Data stack consist of three objects:
- a managed object model
- a managed object context
- a persistent store coordinator
It is key that you understand the role of each of these objects. This diagram illustrates how these objects relate to one another. Take a close look before you continue reading.
Managed Object Model
Every Core Data application has a data model, a file in the application bundle that describes the data of the application. The managed object model, an instance of the NSManagedObjectModel
class, loads the data model and exposes it to the Core Data stack.
When the Core Data stack of the application is set up, the managed object model loads the data model from the application bundle. Even though Core Data is not a database, you can compare the data model with the schema of a database. It describes the data of the application.
Managed Object Context
The managed object context, an instance of the NSManagedObjectContext
class, is the workhorse of the Core Data stack. It is the object of the Core Data stack you, the developer, interact with most.
The managed object context keeps a reference to the persistent store coordinator. Why that is becomes clear in a moment.
As I mentioned earlier, the managed object context is the object of the Core Data stack you interact with most. In fact, it is how you interact with the Core Data stack of the application. You rarely interact with the managed object model or the persistent store coordinator.
Even though most applications have one managed object model and one persistent store coordinator, it is not uncommon for applications to have multiple managed object contexts.
Persistent Store Coordinator
The persistent store coordinator is the glue of the Core Data stack. It keeps a reference to the managed object model and the managed object context. And, as the name implies, the persistent store coordinator is in charge of the persistent store of the application.
Even though the managed object model and the managed object context are indispensable cogs of the Core Data stack, the persistent store coordinator is the heart. It understands the data model of the application through the managed object model and it manages the persistent store of the application.
Creating a Core Data Stack
Now that you know which objects are involved, I am going to walk you through the setup process of the Core Data stack. We won't be focusing on code in this tutorial. Instead, I want to focus on the events that are involved to bring the Core Data stack to life.
Step 1
The first object we need to instantiate is the managed object model. To instantiate an instance of the NSManagedObjectModel
class, we need to load the data model from the application bundle. The data model is used to initialize the managed object model of the Core Data stack.
Step 2
The managed object model is required to instantiate the persistent store coordinator. The persistent store coordinator needs to know and understand the data model of the application before it can add the persistent store of the application.
Step 3
The Core Data stack is only usable once the persistent store is added to the persistent store coordinator. The persistent store coordinator inspects the data model and makes sure the persistent store is compatible with the data model. That is one of the reasons it needs a reference to the managed object model. It uses the managed object model to know about that data model of the application.
Step 4
The application interacts with the Core Data stack through the managed object context. A managed object context keeps a reference to the persistent store coordinator. That is why we first need to create the managed object model and the persistent store coordinator before we can create the managed object context.
Not every managed object context keeps a reference to the persistent store coordinator, but that is a topic for another tutorial.
Working With Data
The managed object context is the workhorse of a Core Data application. It manages a collection of managed objects. If the managed object context needs to load data from the persistent store, it asks the persistent store coordinator for that data. Remember that the persistent store coordinator is in charge of the persistent store. The persistent store coordinator fetches the data the managed object context needs from the persistent store.
If the managed object context wants to save changes to the persistent store, it pushes those changes to the persistent store coordinator. The persistent store coordinator knows how to communicate with the persistent store and pushes the changes to the persistent store.
On iOS, tvOS, and watchOS, the Core Data framework supports three store types:
- a binary store
- an in-memory store
- a SQLite database store
That is everything you need to know for now. I hope you agree that the Core Data stack isn't as complex as it seems. Remember that the Core Data stack consists of three objects and each of these objects has a specific role to play.