Managed Object and Model
The goal of this chapter is to allow users to create a new event when they tap the Add button. To do this, you need to define the Event entity in the managed object model, implement the corresponding class, and create an instance of the class in the add method.
Modeling Your Data
As noted in “The Managed Object Model,” the model is a collection of entity and property description objects that tell Core Data about the managed objects in your application. You can create the model programmatically, or use the Xcode modeling tool to create the model graphically, in a similar way to that in which you create a user interface using Interface Builder.
There are actually several ways to edit the constituent parts of the model; these steps typically describe just one. To learn more about the modeling tool, and other ways to edit the model, see Xcode Tools for Core Data.
This application has just a single entity, an Event, with three attributes—creation date, latitude, and longitude.
Add the Entity
First, add an Event entity.
>> In Xcode, in the Resources group select the model file (
Locations.xcdatamodel) to display the model editor.
>> Choose Design > Data Model > Add Entity to add a new entity to the model.
You can also use the Add button (+) at the lower left of the entity pane, or use the shortcut menu within the diagram view in the model editor.
You should see a new entry for the entity (called “Entity”) appear in the entity pane at the top left of the document editor, and a graphical representation of the entity (a rounded rectangle) appear in the diagram view. Now you can set the name for the new entity.
>> Make sure you have the new entity selected in the entity pane so that you see information about the entity in the detail pane at the right. Change the name of the entity to
Event. (Don’t change the class name.)
Your model should look similar to this:
There’s an important difference between the name of the entity and the name of the Objective-C class used to represent instances of the entity. Core Data uses the entity description to find out about the data objects it manages, so the class name doesn’t have to be the same as the entity name. Indeed, in some cases several entities may be represented by the same class—
NSManagedObject. Core Data is able to differentiate the instances on the basis of their associated entity description.
Add the Attributes
First, add the attribute for the creation date.
>> Make sure you have selected Event in the entity pane, then choose Design > Data Model > Add Attribute.
You should see a new attribute (called
newAttribute) appear in the property pane. You need to set its name and type.
>> Make sure you have selected the new attribute in the property pane, then in the detail pane change the name of the attribute to
creationDate, and select Date from the Type pop-up menu.
You don’t need to set any of the other values.
Now add attributes for latitude and longitude.
>> Make sure you have selected Event in the entity browser, then choose Design > Data Model > Add Attribute twice (to add two attributes).
>> Select both the new attributes in the property pane, then in the detail pane select Double from the Type pop-up menu.
>> Select just the first new attribute in the property pane, and in the detail pane change the Name of the attribute to
>> Select just the second new attribute in the property pane, and in the detail pane change the Name of the attribute to
Your model should look similar to this:
Custom Managed Object Class
Although in principle you can represent an entity using
NSManagedObject, in practice it is common to use a custom class. This yields several benefits, including:
Better support from the development tools.
You get code completion for property accessor methods, and compile-time type- and symbol-checking.
Support for custom methods for the entity.
In many situations, you want to provide special logic for the entity, such as validation methods or derived properties. For example, in a
Personentity you might implement validation method to ensure that a
hasDriversLicenseproperty cannot be true if the
ageproperty is less than 17, or a
fullNamemethod that returns a suitable concatenation of
Now use Xcode to generate the files for a custom class to represent the Event entity.
>>In Xcode, in the model, select the Event entity. (You must select the entity; the selection is used to indicate which items to create subclasses for.)
>>Choose File > New File. In the New File dialog, select Managed Object Class.
Depending on the version of Xcode you’re using, the Managed Object Class may be available in the iOS section under Cocoa Touch Classes, or you may need to choose the template in the Mac OS X section, under Cocoa—either will work correctly.
>>Click Next. The correct location and targets should have been selected for you. Click Next to accept them.
You should see the Entity selection pane, with the Event entity selected. The “Generate accessors” and “Generate Objective-C 2.0 properties” options should also be selected.
>>Click Finish to generate the files.
The Event class interface and implementation files are created and added to your project. There are a few things to notice:
In the interface file (
Event.h), all the attributes are represented by object values.
Although you specified the latitude and longitude attribute types as Double, the property values at runtime are instances of
NSNumber. Core Data uses objects to represent values.
In the implementation file (
Event.m), the properties are implemented as
Normally you might expect to see
synthesized, however Core Data generates the accessor methods at runtime.
In the implementation file (
Event.m), there is no
Normally you might expect to see a
deallocmethod to release instance variables, however Core Data is responsible for the life-cycle of all modeled properties of a managed object. (If you add your own instance variables that do not have corresponding properties in the managed object model, then you need to manage those yourself as normal.)
The model is also updated—the Event entity is now represented by the
Because the model was changed, you need to save it.
>>Save the model file.
Finally, because the table view controller is going to make use of the new class, import its header file in the table view controller’s implementation file.
>>In the table view controller’s implementation file (
RootViewController.m), after the initial import statement, add:
Core Data Recap
You used the Xcode data modeling tool to create a new entity. You then created a custom class to represent that entity. In the next chapter, you’ll create instances of the entity.
If you want to learn more about the modeling tools, see Xcode Tools for Core Data.
© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-12-13)