Chapter 7. Core Data

Core Data is Cocoa’s model and framework for manipulating and storing data. In a nutshell, Core Data provides a nice way to handle relational object persistence without having to worry about the underlying storage.

What do you get from it?

  • Key-value coding and key-value observing

  • Validation

  • Undo/redo support

  • Relationship maintenance

  • Querying, filtering, and grouping

  • Version tracking and optimistic locking

  • Schema migration

  • A memory-optimized solution

  • Integration with Apple’s tool chain (XCode, Interface Builder, and Instruments)

But remember that Core Data is not a database replacement. Even though you can set Core Data to use SQLite as a data store, Core Data doesn’t support sophisticated database operations such as joins. All it supports is the basic CRUD operations. See the Wikipedia entry for CRUD interface. If you are interested in using a database such as SQLite, look at the various Ruby ORMs such as Sequel.

Data Model

At the heart of Core Data is a rich data modeling solution based on simple tools and configuration. The modeling is pretty close to a database design and if you have any experience with databases, you should be able to adapt really quickly.

The best way to understand how Core Data works is to create a simple data model. Let’s build a simple example application: a movie library. At the end of this chapter, we will have built a persisting (saved to disk) movie library that looks like the one shown in Figure 7-1.

Figure 7-1. Movie library using Core Data

Get MacRuby: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.