Understanding Core Data on iOS

Handheld devices such as the iPhone and iPod/Touch are portable repositories for a user’s personal data. The data may be documents, but it often takes the form of databases. For an iOS application to use a database, it relies on a Cocoa API known as “Core Data.” In this article, I examine how Core Data works within an iOS app. I start by explaining how Core Data defines a database structure and what class represents each part of the structure. Then, I detail how Core Data handles a typical file cycle, and how it handles a record transaction. You will need a working knowledge of Objective-C and Xcode a basic understanding of database design.

Core Data and the Database

Core Data gives the iOS app a standard, near-transparent interface to a database file. With Core Data, an app can define a database schema, create a database file, and create and manage record data. Core Data is hardware-agnostic. Runtime support is available on the same processors that MacOS X and iOS support. Plus, Core Data works directly with SQLite, the public-domain database engine bundled with MacOS X and iOS.

Figure 1 shows how a typical database appears under Core Data. The database file is seen as a persistent store. It retains record data between separate app sessions, even after iOS itself shuts down. In memory, the database appears as a managed object context. The context holds a copy of the database schema, and maintains both new and changed records in a queue. It also handles all queries made against the database. The context stays resident only for the lifetime of the app.

Figure 1: How Core Data views a database.

The data flow between context and store is mediated by a coordinator object. Each database table is represented as an entity, each record a managed object. There can be several managed objects within the same entity, and several entities within the same context. A record field is either an attribute or a relation. In the latter type, the field holds a reference to a secondary record.

Core Data and its Classes

At least 20 Cocoa classes make up the whole Core Data framework. Out of those, nine classes play major roles in all database sessions, and it’s important to learn what they do. The following class diagrams show just a subset of methods for each class. Consult the official Apple docs for a complete list.

The NSPersistentStore class (Figure 2) interfaces directly with the database file. It supports three basic file types: SQLite, XML, or a custom binary. It can handle files stored on a local volume or on a network server.

Figure 2: The NSPersistentStore class.

You usually do not create an explicit instance of NSPersistentStore. Instead, use NSPersistentStoreCoordinator (Figure 3) for that task. That class creates the coordinator that links each persistent store to the object context. It serializes the record data, rendering the data into a form suitable for storage. It can move the data from one database file to another. It can work with multiple database files, and it creates a persistent store for each file.

Figure 3: NSPersistentStoreCoordinator.

Leave a comment

Your email address will not be published. Required fields are marked *