Core Data Fundamentals

30 Episodes

Episode 1


Welcome to Core Data Fundamentals

Welcome to Core Data Fundamentals. In this series, you'll learn the ins and outs of Apple's popular Core Data framework. Even though we'll be building an iOS application, the Core Data framework is available on iOS, tvOS, macOS, and watchOS, and the contents of this series apply to each of these platforms.

Episode 2


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.

Episode 3


Building Notes

Notes is a simple application for iOS that manages a list of notes. It's a fine case study to teach you the fundamentals of the Core Data framework. In this episode, I'd like to show you what Notes is capable of.

Episode 5


Creating the Project

Before we set up the Core Data stack, we need to create the project for Notes. Open Xcode and create a new project based on the Single View App template.

Episode 6


Setting Up the Core Data Stack

It's time to write some code. Had we checked the Use Core Data checkbox during the setup of the project, Xcode would have put the code for the Core Data stack in the application delegate. This is something I don't like and we won't be cluttering the application delegate with the setup of the Core Data stack.

Episode 7


Core Data and Dependency Injection

I'm not going to lie. I don't like singletons. Singletons are fine if they're used correctly, but I don't like singletons for convenience. They almost always lead to problems down the line. This means that the Core Data manager isn't going to be a singleton.

Episode 8


Data Model, Entities, and Attributes

The data model is a key component of the Core Data stack and an integral part of a Core Data application. In this episode, we explore the data model and learn about entities and attributes.

Episode 10


Configuring Relationships

Core Data is much more than a database and this becomes clear when you start working with relationships. Relationships in Core Data are powerful because the framework does a lot of the heavy lifting for you.

Episode 11


Working With Managed Objects

Core Data records are represented by the NSManagedObject class, a key class of the framework. In this episode, you learn how to create a managed object, what classes are involved, and how a managed object is saved to a persistent store.

Episode 12


Subclassing NSManagedObject

In the previous episode, we used the NSManagedObject class to represent and interact with records stored in the persistent store. This works fine, but the syntax is verbose, we can't take advantage of Xcode's autocompletion, and type safety is also an issue.

Episode 13


Adding a Note

In the next episodes, we add the ability to create, read, update, and delete notes. Before we start, we need to make some preparations.

Episode 14


Don't Forget to Save

You may be wondering why we didn't save the note immediately after creating it. That's a fair question. We could have. But why would we? Why would we push the changes of the managed object context to the persistent store every time something changes in the managed object context? That's a waste of resources and it may even impact performance, depending on the complexity of the operation.

Episode 15


Fetch Those Notes

In this episode, we fetch the user's notes from the persistent store and display them in a table view. The notes view controller is in charge of these tasks.

Episode 16


Fix That Mistake

The application we're building wouldn't be very useful if it didn't include the ability to edit notes. Would it? If the user taps a note in the notes view controller, they should be able to modify the title and contents of the note.

Episode 17


To the Trash Can

The application currently supports creating, reading, and updating notes. But it should also be possible to delete notes. The pieces we need to add the ability to delete notes are already present in the notes view controller. In fact, we only need to implement one additional method of the UITableViewDataSource protocol to add support for deleting notes.

Episode 19


Exploring the NSFetchedResultsControllerDelegate Protocol

To update the table view, we listened for notifications sent by the managed object context of the Core Data manager. This is a perfectly fine solution. But it can be messy to sift through the managed objects contained in the userInfo dictionary of the notification. In a complex Core Data application, the NSManagedObjectContextObjectsDidChange notification is sent very frequently. It includes every change of every managed object, even the ones we may not be interested in. We need to make sure we only respond to the changes of the managed objects we are interested in.

Episode 20


Adding Categories to the Mix

To help users manage their notes, it's helpful to allow them to categorize their notes. Earlier in this series, we added the Category entity to the data model. Remember that a note can belong to only one category, but a category can have many notes. In other words, the Note and Category entities have a One-To-Many relationship. The data model editor visualizes this.

Episode 21


Adding a Dash of Color

In this episode, I'd like to add the ability to assign a color to a category. This makes it easier to visualize which category a note belongs to.

Episode 22


Data Model Migrations

An application that grows and gains features also gains new requirements. The data model, for example, grows and changes. Core Data handles changes pretty well as long as you play by the rules of the framework.

Episode 24


Assigning Tags to a Note

The last feature I want to add is the ability to tag notes. This feature is interesting because of the Many-To-Many relationship of the Note and Tag entities. In this episode, you learn how to work with such a relationship.

Episode 25


Working In a Multithreaded Environment

We currently use one managed object context, which we created in the CoreDataManager class. In the application, we pass the managed object context to the objects that need it. This works fine, but there will be times when one managed object context won't cut it.

Episode 28


Replacing the Core Data Manager Class

The CoreDataManager class is in charge of the Core Data stack of the application. It encapsulates the Core Data stack and only exposes the main managed object context to the rest of the application.

Episode 29


Understanding Faulting

In this episode, I'd like to discuss a concept that often confuses developers new to Core Data, faulting. Before I explain what faulting is, I want to show it to you.

Episode 30


Where to Go From Here

My hope is that this series has taught you that Core Data isn't as difficult as many developers believe it to be. Core Data isn't complex once you understand how the various pieces of the framework fit together.