Core Data is one of the most misunderstood frameworks available on iOS, tvOS, macOS, and watchOS. And that is unfortunate because it is a fantastic piece of software engineering. But why is it misunderstood?
Developers very often don't take the time to learn the framework and they don't know what Core Data is before they start using it in a project. I am a fervent advocate of learning by doing, but sometimes you first need to take a step back. In this article, I show you the roadmap I use for my students.
The Core Data Roadmap
After having taught thousands of students, I understand which topics developers struggle with most and that is what led to the creation of the Core Data roadmap. It is this roadmap I use in Mastering Core Data With Swift.
Step 1: What Is Core Data
Before we start exploring the framework itself, you need to understand what Core Data is. And, more importantly, what Core Data is not. Some developers use Core Data because they need a SQLite wrapper. That is not what Core Data is. If you need a SQLite wrapper, I highly recommend Gus Mueller's FMDB library.
Core Data is not a database, it is much more than that. The framework manages an object graph and is very good at it. It offers a slew of features, such as validation and undo management.
Step 2: Practice, Practice, Practice
As I mentioned earlier, I am an advocate of learning by doing and that is what I recommend for learning the Core Data framework. Pick a simple problem you would like to solve with Core Data and try to solve it.
In Mastering Core Data With Swift, you build an application to manage notes. This is a great fit for the course because it teaches you how to create, read, update, and delete (CRUD) records and it also shows you how to manages relationships using Core Data.
Step 3: The Heart of Core Data
The biggest hurdle developers need to take is understanding the Core Data stack. I promise you that the Core Data stack is not difficult to learn or set up. But you need to make sure you understand how the various pieces fit together. Don't skip this step. It is the most important step on your path to mastering Core Data.
In Mastering Core Data With Swift, I also teach you a few best practices. For example, you create a separate class that is responsible for the Core Data stack. Apple's Core Data template dumps the logic for setting up the Core Data stack in the application delegate. Don't do that.
We also use dependency injection to pass the managed object context to the objects that need to access it. Dependency injection is such an improvement over the singleton pattern. Give it a try.
Step 4: The Brain of Core Data
Every persistence framework has a data model of some sort and Core Data is no different. Xcode ships with a fantastic data model editor that makes it trivial to create the data model of your application, version the data model, and prepare migrations. It has been around for many years and does a terrific job.
The data model is not difficult to understand as long as you take the time to learn the subtle details of the framework, such as delete rules and inverse relationships.
Step 5: Working With Managed Objects
NSManagedObject
is the class you interact with most in a Core Data application. Fortunately, working with managed objects is very straightforward. The class has several advanced features, but most are aimed at more advanced uses of Core Data.
Fetching, reading, updating, and deleting records is not difficult. You can also explore the NSFetchedResultsController
class. This class makes it easy to manage the results of a fetch request and keep the user interface up to date. We cover this in detail in Mastering Core Data With Swift.
Step 6: Core Data and Concurrency
Most applications need to work on multiple threads. The Core Data framework understands how to operate in a multithreaded environment, but you need to know which rules to respect to avoid problems.
Even though concurrency may seem an advanced topic, I consider it fundamental to understanding Core Data. It isn't difficult, but you need to know the rules Core Data lives by.
Avoid the Struggle and Take Your Time
By taking the time to learn Core Data, you save yourself a lot of time and frustration. Core Data isn't UIKit or Foundation. You cannot pick and choose which components you want to use. Core Data has a strict set of rules you shouldn't break. If you take the time to learn those rules, you'll be fine.
If you are committed to learning Core Data, then you should take my course, Mastering Core Data With Swift. In less than three hours, you learn what you need to know about the framework to integrate it in your projects.