What Is Core Data?
Welcome to Colour Me Cocoa.
Looks like you're new here. If you like what you see, you may want to subscribe to my RSS feed, or if you prefer sign up to receive email updates.
Thanks for visiting!
Apple’s developer tools are based around a design pattern known as the Model-View-Controller (MVC). The simplest definition of MVC is to keep your data and the rules for using it (the model) separate from your view (the user interface) and to then tie them together using your Controller. The idea is that in doing so it becomes possible to re-use parts of your code on other projects.
When we built our first cocoa application our view was built in Interface Builder (using pre-built user interface components), while our controller code was written in Xcode using cocoa bindings. Our first cocoa application did not have a model.
Core Data is Apple’s framework for developing your applications data store. Lets take a look at what it can do for you and your applications.
Building A Data Store
Nearly every application I have ever worked on has required some form of data store. This is usually done by serializing your data objects into a text file using your format of choice.
Reading and writing data to and from text files is a lot of work. You have to translate your data objects into text, you also have to translate text back into data. On top of this you need to make sure all the data is deconstructed and re-assembled in the correct order and that the text file itself has not been corrupted by a nosey user editing it directly.
Core Data makes working with data simple. Open up Xcode and create a new project. Select “Core Data Application” from the new project assistant (it’s under the Application heading). I’ve called mine coreDataTest.
Open up the coreDataText_DataModel.xcdatamodel file by double clicking on it. Xcode will open up a window you probably haven’t seen before. This is the window in which you can edit core data models.
In the top left box you can add an entity, the box to the right of it is where you can edit your new entities properties. The panel on the far right lets you set all of the options related to an entity while the grid paper area below shows a visual representation of your data model.
There are four types of properties you can give to an entity, but we are going to focus on the following two today:
- Attributes
- Relationships
Attributes are the values that make up your entity. A person for example might have a firstName, a surname and an age field. This is exactly like building a table in a database and naming the columns. Use the plus symbol to add a person entity with the above properties.
A relationship is a connection between two entities. A person has a parent, we can represent this in our core data model by adding a relationship called parent with a Destination of Person. Go ahead and add a relationship in the same way you added attributes. You should end up with something similar to the following:
That was easy wasn’t it? Our model here is extremely simple, but it should provide you with enough to get started.
Creating A User Interface For A Core Data Model Quickly
Creating a user interface from a core data model is equally simple. Open up Interface Builder. Option-Click the visual representation of your model and drag it onto a new window in Interface Builder. Elect to have a User Interface for many person objects created. And you’re done!
Save your Interface Builder files and use Xcode to build and run your application. You didn’t write any code, yet you have a fully functioning application that that remembers its state and has built in undo/redo functionality. That’s really powerful!
That being said, you probably wouldn’t want to release your Interface Builder generated user interface as part of your next killer application. But it’s useful when trying to see how your model works and if there is something missing.
Want To Learn More?
No doubt you will want to learn more about Core Data. The following two links from the ADC should get you started. If anyone knows of any other Core Data tutorials that are worth reading then let us all know in the comments and I’ll add them to the list.
Updated: 8th May 2007
Reason: Re-wrote the MVC definitions to be more concise and accurate.




May 7th, 2007 at 7:06 pm
In MVC typically the business logic belongs in the Model, does Cocoa make the distinction that this belongs in their controllers?
May 7th, 2007 at 10:02 pm
Ryan that’s true about MVC and I’ve updated the article.
Thanks for pointing out that mistake.