Adding Core Data to Your App

Core Data is Apple's data persistence framework. You can utilize this framework whenever your application needs to store data. Simple data can often be stored in UserDefaults, but when you're handling data that's more complex, has relationships, or needs some form of efficient searching, Core Data is much better suited to your needs.

You don't need to build a very complex app or have vast amounts of data to make Core Data worth your while. Regardless of your app's size, even if it's tiny with only a couple of records, or if you're holding onto thousands of records, Core Data has your back.

In this chapter, you'll learn how to add Core Data to an existing app. The app you will build keeps track of a list of favorite movies for all members of a family. The main interface is a table view that shows a list of family members. If you tap on a family member, you'll see their favorite movies. Adding family members can be done through the overview screen, and adding movies can be done through the detail screen.

You won't build the screens in this app from scratch. The code bundle for this chapter includes a starter project called MustC. The starter project contains all of the screens, so you don't have set up the user interface before you get around to implementing Core Data.

In this chapter, the following topics are covered:

  • Understanding the Core Data Stack
  • Adding Core Data to an application
  • Modeling data in the model editor
  • Storing and querying data
  • Using multiple managed object contexts