Core Data simplified

Core Data can appear overwhelming to the new iOS programmer. This post attempts to distil and simplify a number of concepts.

.

This post was most recently updated in February 2012.

.

Core Data object design

The following table shows the essential Core Data object design information.

Concept Answer
How do you create a Core Data model? Use the Core Data Model Editor in Xcode
What’s in a model? One or more entities, designed by you
An entity is a class, a template for objects
The entity class has properties (attributes and relationships)

.

Core Data tasks/operations

The following table shows the essential Core Data tasks/operations.

Task / operation Answer
What do you do if you want a new object? Create a new managed object
What do you do if you want to work with one or more objects? Configure and perform a fetch request; a fetch request often has a predicate (i.e. a filter) and sort descriptors
What do you do when you’re done adding or modifying an object, and want to save it? Save the changes to the context

.

Core Data objects

The following table shows the essential Core Data objects in an app.

Core Data object Where is it in your app
Where is the Core Data “stack” initialized? Your model class
What components are initialized? MOM – managed object model
PSC – persistent store coordinator
MOC – managed object context
When does the model object get initialized? In the app delegate. Then, the app delegate passes on a reference to the model object to the first/root view controller.
How do you access Core Data functionality in a view controller? Make sure that you have a reference to the model object
What is the name of the special-purpose object that wraps a fetch request, and is designed to be used with a UITableView? FRC – Fetched results controller

.

The “important things to know” about Core Data

The problem that Core Data solves is the overall design, management, and persistence of your app’s data objects.

.

Start with Empty Application (and add the CoreData.framework yourself).

.

Use consistent names for the object model (ObjectModel.xcdatamodeld) and object store (ObjectStore.sqlite).

.

The managed object context is an important class – it’s the work area for your objects. The fetched results controller is an important class. It wraps a fetch request, which is another important class. To use a fetched results controller in a view controller, you must configure it, and tell it to perform the fetch.

.

A model object is your friend, because it becomes an easy-to-use interface to your app’s data.

.

Core Data enables us to work with objects that model our real-world data. The objects have properties which are either attributes (state) or relationships (to other objects). Simply use dot notation to dereference related objects and their properties.

.

The syntax for working with Core Data is dictionary-like (e.g. “valueForKey”). Can add a category to enable setters or to be able to use declared property accessor dot syntax. Alternatively, can generate NSManagedObject subclasses to make the syntax even clearer.

.

When working with multiple view controllers, make sure they are configured with a model object property; also, pass on a reference to the object that they are interested in.

.

For view controllers that modify data, they must implement the fetched results controller delegate protocol.

.

Getting started with Core Data

Use a search engine, and the search words suggested below, to get quick access to these documents. Then print the important content, and annotate with your notes and observations.

Core Data Basics

Core Data Model Editor Help

NSManagedObject Class Reference

NSFetchRequest Class Reference

NSManagedObjectContext Class Reference

NSEntityDescription Class Reference

NSFetchedResultsController Class Reference

.


.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: