About the book

Practical Core Data is a book aimed at intermediate to advanced developers who want to learn more about Apple's Core Data framework. Whether you're new to Core Data, or are a veteran that tried Core Data years ago, you'll find that this book will take you on a journey through the framework. From iOS 10's NSPersistentContainer all the way to features like CloudKit synchronization, migrations and unit testing. You will see it all in this book.

Practical Core Data introduces you to all the essential ideas, concepts and principles to get you up and running with the framework as soon as possible.


Chapter overview

Chapter 1 - Taking your first steps with Core Data

In this chapter, you will take your first look at Core Data. You will learn what Core Data is, what it can do, and how it can be used. You will be introduced to a simple project that uses Core Data to create, update, and delete a very basic model. This chapter will introduce Core Data's model editor as well as the `NSPersistentContainer` class that encapsulates all of Core Data's essential building blocks. By the end of this chapter, you should have a rough idea of what an application that uses Core Data looks like, what it's capable of, and how you can add Core Data to a project.

Chapter 2 - Understanding Core Data's building blocks

Before we get around to using Core Data in an application, it's important to understand some of Core Data's essential building blocks. In this chapter, you will learn which components are inside an `NSPersistentContainer`, and what the role of each component is. You will learn about classes such as `NSPersistentStoreCoordinator`, `NSManagedObjectModel`, and `NSManagedObjectContext`.

Chapter 3 - Defining entities using the model editor

Core Data's model editor is the starting point for adding Core Data to any project. It's where you define the entities that you want to store, their relationships, their properties, and more. This chapter will provide an in-depth look at the model editor. You will learn about entities, relationships, fetch requests, transient properties, constraints, model versions, and much more. By the end of this chapter, you will have a solid understanding of the model editor and how you can use it to define your data models.

Chapter 4 - Fetching and displaying data from a Core Data store

Once you have defined a data model for your application, you'll want to use this model in your application. In this chapter, you will learn how you can retrieve data from a Core Data store using predicates to filter data. You will also learn how you can automatically respond to changes in your Core Data store using an `NSFetchedResultsController`. In addition to learning more about retrieving data, you will also learn about faults, batch delete and update requests, merge policies, and query generations. By the end of this chapter, you should have a pretty solid understanding of how you can manipulate the data that you have stored in your Core Data store.

Chapter 5 - Using Core Data in a multithreaded environment

In the first four chapters, you've used Core Data in a simple, single-threaded environment. You've only interacted with the main managed object context through the persistent container's `viewContext`. In this chapter you will learn about Core Data's strict multithreading rules and background contexts. You will learn how you can safely transfer a managed object from one context to the other, how you can solve save conflicts between contexts, and how you can safely run your Core Data code in the correct managed object context. By the end of this chapter you will be able to optimize your Core Data code to make optimal use of threading, and to prevent blocking the main thread at all times.

Chapter 6 - Sharing a Core Data store with apps and extensions

Many applications make use of iOS' extension system to build widgets, interactive notifications, share functionality, iMessage apps and more. If your app uses Core Data, it's often desirable that these extensions have access to your Core Data store and can make modifications if needed. In this chapter you will learn how you can do this. You will also learn how you can use Core Data's persistent history tracking features to automatically pull in any modifications that were made to your Core Data store by one of your extensions.

Chapter 7 - Synchronizing your store with a remote data source

A common use of Core Data is to locally persist data that also exists on a server that's accessed over the network. Doing this can dramatically increase your application's speed and offline experience because you don't need to rely on the network for every piece of data your views require. In this chapter, I will demonstrate how you can build a synchronization strategy that uses the `Codable` protocol and Core Data's built-in merge policies. After showing you a basic one-way synchronization strategy I will show you a more advanced two-way synchronization strategy that allows users to make modifications to records while they are offline. These modifications are then pushed to a remote data source when the user is back online.

Chapter 8 - Synchronizing your store with CloudKit

With iOS 13 Apple introduced a convenient way for developers to synchronize their local Core Data stores with a remote CloudKit store with hardly any effort at all. This feature allows users to synchronize data across their devices and Core Data does all the hard work on its own. In this chapter you will learn how you can leverage this feature, and how you can write data to CloudKit’s private and public storage

Chapter 9 - Updating your data model and performing migrations

This chapter covers everything you need to know about updating your Core Data model. You will learn about Core Data's automatic migrations, model mapping, model versions, and more. After understanding the basics of automatic migrations you will take a look at how you can write your own model mappings. To top it off you will learn how you can take full control of Core Data's migration process by writing your own migration logic from scratch.

Chapter 10 - Debugging and profiling your Core Data implementation

Knowing how you can use and integrate Core Data in a project is extremely useful, but knowing how to profile and debug it can be the key to taking your code from good to great. This chapter introduces you to some launch arguments that you can use to gain insights into what Core Data does under the hood, as well as profiling your code with instruments. You will learn how to use the information gained from Core Data's logging and Instruments to improve and optimize your Core Data code and the user's experience.

Chapter 11 - Using Core Data in your unit tests

I firmly believe that every developer should write unit tests for their code whenever they can. In this chapter I provide some guidance on how you can correctly set up and use a Core Data store in your unit tests. This will allow you to write your unit tests in isolation without interfering with each other by setting up a separate, temporary, store for each unit test.

Chapter 12 - Where to go from here?

This book should set you up for a successful Core Data implementation in your applications. You learned Core Data's basics in the first couple of chapters and moved on to more complex use cases and concepts in later chapters. And while I've done my best to cover a lot, there is always more to learn. In this chapter, I will recommend articles, books, and videos that I think are useful to look at if you want to learn more about Core Data when you're done reading this book.


Other books by Donny Wals

Practical Combine

Practical Combine banner

Practical Combine is a book aimed at intermediate to advanced developers who want to learn more about Apple's Combine framework. This book takes you all the way from the basics to building custom Combine publishers using Practical, useful examples that you can start using immediately.

Learn more