What is Core Data? You’ll hear a variety of answers to this question: It’s a database! It’s SQLite! It’s not a database! And so forth.

Here’s the technical answer: Core Data is an object graph management and persistence framework in the macOS and iOS SDKs.

That means Core Data can store and retrieve data, but it isn’t a relational database like MySQL or SQLite. Although it can use SQLite as the data store behind the scenes, you don’t think about Core Data in terms of tables and rows and primary keys.

Imagine you’re writing an app to keep track of dining habits. You have a varied set of objects: restaurant objects, each with properties such as name and address; categories, to organize the restaurants; and visits, to log each visit to a restaurant.

The object graph in memory might look something like this:

Object graph management means Core Data works with objects that you define, such as the ones in the diagram above. For example, each restaurant (represented by a red bubble) would have a property pointing back to the category object. It would also have a property holding the list of visits.

Since UIKit is an object-oriented framework, you’re probably storing data in objects already. Core Data builds on this to keep track of the objects and their relationships to each other. You can imagine expanding the graph to include what the user ordered, ratings and so on.

Persistence means the data is stored somewhere durable such as the device’s flash memory or “the cloud.” You point to the entire graph and just say “save.”

When your app launches, you just say “load” and the entire object graph pops up in memory again, ready for use. That’s Core Data at work!

Maybe your users eat out a lot and have thousands of restaurant visits — rest assured Core Data is smart about lazily loading objects and caching to optimize both memory usage and speed. Core Data has many other features aside from simply storing and fetching data:

You can perform custom filtering with predicates, sort the data and and calculate statistics. You’ll learn all about these features and more in this book.

We’ve updated all the chapters in this book for Swift 5.3, iOS 14 and Xcode 12.

Who this book is for

This book is for iOS developers who already know the basics of iOS and Swift, and want to learn Core Data.

If you’re a complete beginner to iOS, we suggest you read through The iOS Apprentice first. That will give you a solid foundation in building iOS apps from the ground-up.

If you know the basics of iOS development but are new to Swift, we suggest you read Swift Apprentice first. That book has a similar hands-on approach and takes you on a comprehensive tour through the Swift language.

You can find both of these prerequisite books at our store: http://store.raywenderlich.com

How to use this book

This book will teach you the fundamentals of Core Data by means of hands-on tutorials. You’ll jump right into building a Core Data app in Chapter 1, as we think most people learn best by doing. We encourage you to type along with the instructions in the book.

If you’re new to Core Data or want to review the basics, we suggest you start with Chapters 1–3. These chapters cover the fundamentals of Core Data and you’ll need the knowledge in them to understand the rest of the book.

Otherwise, we suggest a pragmatic approach. Each chapter stands on its own, so you can pick and choose the chapters that interest you the most.

What’s in store

Here’s a quick summary of what you’ll find in each chapter:

1. Chapter 1, Your First Core Data App: You’ll click File ▸ New Project and write a Core Data app from scratch! This chapter covers the basics of setting up your data model and then adding and fetching records.

2. Chapter 2, NSManagedObject Subclasses: NSManagedObject is the base data storage class of your Core Data object graphs. This chapter will teach you how you customize your own managed object subclasses to store and validate data.

3. Chapter 3, The Core Data Stack: Under the hood, Core Data is made up of many parts working together. In this chapter, you’ll learn about how these parts fit together, and move away from the starter Xcode template to build your own customizable system.

4. Chapter 4, Intermediate Fetching: Your apps will fetch data all the time, and Core Data offers many options for getting the data to you efficiently. This chapter covers more advanced fetch requests, predicates, sorting and asynchronous fetching.

5. Chapter 5, NSFetchedResultsController: Table views are at the core of many iOS apps, and Apple wants to make Core Data play nicely with them! In this chapter, you’ll learn how NSFetchedResultsController can save you time and code when your table views are backed by data from Core Data.

6. Chapter 6, Versioning & Migration: As you update and enhance your app, its data model will almost certainly need to change. In this chapter, you’ll learn how to create multiple versions of your data model and then migrate your users forward so they can keep their existing data as they upgrade.

7. Chapter 7, Unit Tests: Testing is an important part of the development process, and you shouldn’t leave Core Data out of that! In this chapter, you’ll learn how to set up a separate test environment for Core Data and see examples of how to test your models.

8. Chapter 8, Measuring & Boosting Performance: No one ever complained that an app was too fast, so it’s important to be vigilant about tracking performance. In this chapter, you’ll learn how to measure your app’s performance with various Xcode tools and then pick up some tips for dealing with slow spots in your code.

9. Chapter 9, Multiple Managed Object Contexts: In this chapter, you’ll expand the usual Core Data stack to include multiple managed object contexts. You’ll learn how this can improve perceived performance and help make your app architecture less monolithic and more compartmentalized.

10. Chapter 10, NSPersistentCloudKitContainer: Your users may have multiple devices, and they’d love your app to run on them all, and for the data to be shared between them. In this chapter you will learn how to get iCloud-backed data synchronization (almost) for free using NSPersistentCloudKitContainer.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.

Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:

© 2021 Razeware LLC