If you dipped your toe in iOS or macOS development, then you've probably heard about Core Data. Core Data is Apple's framework for managing and persisting an object graph. It was introduced in 2005, alongside the release of Mac OS X Tiger. On iOS, the framework has been available since iPhone OS 3.0.
Core Data has earned its stripes as a reliable and performant persistence layer. It's been around for more than a decade and it's used in thousands of iOS and macOS applications. Apple continues to invest in Core Data, adding features, increasing performance, and improving reliability with every iteration of the framework.
First Things First
Developers new to Core Data are often overwhelmed by the framework's terminology and the plethora of classes defined by the framework. To get a good grasp of Core Data, a solid foundation is instrumental. Don't skip this step.
To get a good grasp of Core Data, a solid foundation is instrumental. Don't skip this step.
Before you start using the framework in a project, it's important to take the time to become familiar with the Core Data stack and the framework's key classes. We cover this extensively in Mastering Core Data With Swift.
In this tutorial, I explain what you need to know about Core Data to learn the basics of this amazing framework. We start by exploring the Core Data stack and discussing the key players of the framework.
What Is Core Data?
Asking someone to define Core Data is a bit of a trick question. It's hard to define Core Data because it behaves like a database while Apple clearly states that it isn't a database. The definition I often use is short and simple.
Core Data is a framework for managing and persisting an object graph.
It's as simple as that. While Core Data does quite a bit more, it's important that you understand the objective of the framework. Don't get too attached to this definition, though. What Core Data is and isn't becomes clear along the way. Let's first discuss how Core Data works by exploring the Core Data stack.
What Is the Core Data Stack?
Now that you have an idea of what Core Data is, it's time to zoom in on the building blocks of the Core Data framework. As I mentioned earlier, it's key that you understand how the various classes that make Core Data tick play together.
The star players of the Core Data framework are the persistent store coordinator, the managed object model, and the managed object context. The following diagram shows the relationships of these objects. We use this diagram as a guideline in this tutorial.
Managed Object Model
The managed object model is an instance of the NSManagedObjectModel
class. A typical Core Data application has one instance of the NSManagedObjectModel
class. The instance represents the data model of the Core Data application.
The above diagram shows that the managed object model is connected to the data model. The data model is represented by a file in the application bundle that contains the data schema of the application. The data schema is nothing more than a collection of entities. An entity can have attributes and relationships, which make up the data model of the application.
We explore the data model in more detail later. For now, remember that the managed object model is an instance of the NSManagedObjectModel
class and represents the data model of the Core Data application.
Managed Object Context
A Core Data application has one or more managed object contexts. Each managed object context manages a collection of model objects, instances of the NSManagedObject
class. The managed object context receives the model objects through a persistent store coordinator. In the above diagram, you can see that every managed object context has a reference to the application's persistent store coordinator.
The managed object context is the object you interact with most. The managed object context creates, reads, updates, and deletes model objects. From a developer's perspective, the NSManagedObjectContext
class is the workhorse of the Core Data framework.
Persistent Store Coordinator
The persistent store coordinator plays a key role in every Core Data application. While it is possible to have multiple persistent store coordinators, most applications have only one. The persistent store coordinator keeps a reference to the managed object model and every parent managed object context keeps a reference to the persistent store coordinator. What is a parent managed object context? Later in this series, we take a closer look at parent and child managed object contexts.
The above diagram also tells us that the persistent store coordinator is connected to one or more persistent stores. What is a persistent store? Remember that Core Data manages an object graph. The framework is only useful if the persistent store coordinator is connected to one or more persistent stores. Out of the box, Core Data supports three persistent store types, a SQLite database, a binary store, and an in-memory store.
Each persistent store type has its pros and cons. Most applications use a SQLite database as their persistent store. SQLite is lightweight and very fast. It is great for mobile and desktop applications.
How Does Core Data Work?
Now that we know what the Core Data stack consists of, it is time to explore how the stack operates in an application. The heart of a Core Data application is the persistent store coordinator. The persistent store coordinator is created first when the Core Data stack is set up. To create the persistent store coordinator, however, a managed object model is required. Why is that? The persistent store coordinator needs to know what the data schema of the application looks like.
After setting up the persistent store coordinator and the managed object model, the workhorse of the Core Data stack is initialized, the managed object context. Remember that every managed object context keeps a reference to the persistent store coordinator.
With the Core Data stack set up, the application is ready to use Core Data to interact with the application's persistent store. In most cases, an application interacts with the persistent store through the managed object context. You rarely, if ever, directly interact with the persistent store coordinator or the managed object model. As I mentioned earlier, the NSManagedObjectContext
class is the class you interact with most frequently.
The managed object context is used to create, read, update, and delete records. When the changes made in the managed object context are saved, the managed object context pushes them to the persistent store coordinator, which sends the changes to the persistent store.
Now that you know what Core Data is and how the Core Data stack is set up, it's time to write some code. If you're serious about Core Data, check out Mastering Core Data With Swift. We build an application that is powered by Core Data and you learn everything you need to know to use Core Data in your own projects.