Core Data Fundamentals

What Is Core Data

Developers new to Core Data usually don't take the time to learn the framework. Not knowing what Core Data is, makes it hard and frustrating to wrap your head around the ins and outs of the framework. I'd like to start by spending a few minutes exploring the nature of Core Data and, more important, explain to you what Core Data is and isn't.

Core Data is a framework developed and maintained by Apple. It's been around for more than a decade and first made its appearance on macOS with the release of Mac OS X Tiger in 2005. In 2009, the company made the framework available on iOS with the release of iPhone OS 3. Today, Core Data is available on iOS, tvOS, macOS, and watchOS.

Core Data is the M in MVC, the model layer of your application. Even though Core Data can persist data to disk, data persistence is actually an optional feature of the framework. Core Data is first and foremost a framework for managing an object graph.

You've probably heard and read about Core Data before watching this episode. That means that you may already know that Core Data is not a database and that it manages your application's object graph. Both statements are true. But what do they mean?

Core Data Manages an Object Graph

Remember that Core Data is first and foremost an object graph manager. To understand what that means, you need to know what an object graph is.

An object graph is nothing more than a collection of objects that are connected with one another. The Core Data framework excels at managing complex object graphs.

The Core Data framework takes care of managing the life cycle of the objects in the object graph. It can optionally persist the object graph to disk and it also offers a powerful interface for searching the object graph it manages.

But Core Data is much more than that. The framework adds a number of other compelling features, such as input validation, data model versioning, and change tracking.

Even though Core Data is a perfect fit for a wide range of applications, not every application should use Core Data.

When to Use Core Data

If you're in need of a lightweight model layer, then Core Data shouldn't be your first choice. There are many lightweight libraries that provide this type of functionality.

And if you're looking for a SQLite wrapper, then Core Data is also not what you need. For a lightweight, performant SQLite wrapper, I highly recommend FMDB or SQLite.swift. These robust libraries provide an object-oriented interface for interacting with SQLite.

Core Data & SQLite

Core Data is an excellent choice if you want a solution that manages the model layer of your application. Developers new to Core Data are often confused by the differences between SQLite and Core Data.

If you're wondering whether you need Core Data or SQLite, then you're asking the wrong question. Remember that Core Data isn't a database.

SQLite is a lightweight database that's incredibly performant, and, therefore, a good fit for mobile devices. Even though SQLite is advertised as a relational database, it's important to realize that the developer is in charge of maintaining the relationships between records stored in the database.

Core Data Goes Much Further

Core Data provides an abstraction that allows developers to interact with the model layer in an object-oriented manner. Every record you interact with is an object.

Core Data is responsible for the integrity of the object graph. It ensures the object graph is kept up to date.


Core Data is a fantastic framework, but there are several drawbacks. These drawbacks are directly related to the nature of Core Data and how it works under the hood.


Core Data can only do its magic because it keeps the object graph it manages in memory. This means that it can only operate on records once they are in memory. This is very different from performing a SQL query on a database. If you want to delete thousands of records, Core Data first needs to load each record into memory. It goes without saying that this results in memory and performance issues if done incorrectly.


Another important limitation is the threading model of Core Data. The framework expects to be run on a single thread. Fortunately, Core Data has evolved dramatically over the years and the framework has put various solutions in place to make working with Core Data in a multithreaded environment much safer and much easier.

For applications that need to manage a complex object graph, Core Data is a great fit. If you only need to store a handful of unrelated objects, then you may be better off with a lightweight solution or the user defaults system.

Next Episode "Building Notes"

Stop Writing Swift That Sucks

Download the Swift Patterns I Swear By