Core Data Knowledge

Kip Landergren

(Updated: )

My Core Data knowledge base explaining models, objects, and persistence.

Contents

Overview

Core Data is an Apple-provided framework for defining, managing, and persisting an application’s data. This data takes the form of an object graph where the structure of its contained entities is defined by a Managed Object Model. Instances of these entities are able to be created, read, updated, and deleted, all with safe access and robust persistence.

Documentation tends to stress that Core Data is not a database. It is an object graph persistence framework that can be backed by a database, among other options.

The convenience to the programmer is that they can largely sequester the logic for defining data structures and performing read/write operations from working programmatic objects. This keeps business logic:

and permits application logic to safely integrate it with minimal conceptual distance. Additionally almost all functionality is callable via Swift and Objective-C which allows for staying inside a familiar runtime.

Core Idea

Provide built-in and performant object graph persistence. Abstract away the mechanism of persistence (SQLite by default) and provide the programmer with the means of:

Key Concepts

Models and Objects

Models define the structure of your data objects; they can be loosely thought of as akin to database tables. These are represented by NSEntityDescription objects and are maintained by a NSManagedObjectModel.

Objects are realized instances of your models—meaning they exist in memory as programmatic objects—that you can integrate into your application.

The whole use of “managed” is to connote to the programmer that Core Data has a hand in the life cycle of the object. It animates objects to life from raw data in a persistent store and provides a marionette’s theater—the managed object context—through which the programmer can interact with them.

Managed Object Model

A managed object model defines the schema and relationships of your object graph. It is configurable programmatically or through Xcode’s Data Model Editor, backed by a .xcdatamodeld file.

Managed Object

Xcode provides multiple options for generating code that represents the entities defined in your managed object model. The default—generating the classes for you—gives a straightforward connection between what is defined in the data editor and what is available in code. Other options provide opportunities for more control.

Persistence

Persistence is the act of saving data over time. Core Data abstracts the mechanism of persistence behind the concept of a “store”, or place that holds data. Many of them may exist in an app! And need to evolve versions over time. Safe access to these stores is managed by a store coordinator; it ensures that, from the caller’s perspective, they are appear to be dealing with a single, aggregate, store.

Persistent Store

An NSPersistentStore is the means by which Core Data maintains data of an application’s object graph.

There are four types:

Choice of the backing store depends on your application’s data use.

Persistent Store Coordinator

The NSPersistentStoreCoordinator presents a façade to managed object contexts such that multiple stores can be accessed safely.

Putting it all Together

Managed Object Context

An NSManagedObjectContext can safely create an instance of the object graph for use in application logic. This view of the object graph is internally consistent and provides access to managed objects for manipulation and the means of persisting them through save().

Related

External Resources