01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Core Data history<br />

229<br />

Structured Query Language (SQL). It was the primary method for persisting (or storing)<br />

data on the iPhone. It got the job done, but it required a lot of overhead during<br />

development. To use SQLite, you had to do everything “by hand”: You had to create<br />

and initialize the database. You had to write SQL code for all CRUD (create, read,<br />

update, and delete) operations. You had to manually set up and manage relationships.<br />

And as a good object-oriented developer, you had to put the data you got from the<br />

database into <strong>Objective</strong>-C data model objects and take data from those objects again<br />

when you wanted to build a database query that updated, created, or deleted data. On<br />

top of that, you had to manage primary and foreign keys, and so on and so forth. You<br />

get the picture. It was no fun at all to write all that code.<br />

Fortunately, Apple realized that too and brought the Core Data framework to the<br />

iPhone with the 3.0 software development kit (SDK). Core Data can be compared to<br />

ActiveRecord (if you know Ruby on Rails) or Hibernate (if you’re a Java developer). It<br />

handles all the low-level nitty-gritty details of data persistence for you: you don’t have<br />

to write SQL code, you don’t have to map between data and data objects, and you<br />

don’t have to handle relationships (it won’t fix your marriage, though).<br />

In this chapter, we take a high-level look at all the different moving parts of Core<br />

Data and then get our hands dirty by building a simple Core Data application. Please<br />

keep in mind that Core Data is an advanced topic, and it would take a whole book to<br />

cover everything there is to know about it. But this chapter teaches you everything you<br />

need to know to leverage Core Data for all your most common data persistence needs.<br />

12.1 Core Data history<br />

Nearly every application has a data model and model objects (people, invoices, products,<br />

departments—you name it). And nearly every application has to save some or all<br />

of that data to disk. Functionality that is needed by a substantial number of applications<br />

and solutions to problems that developers face repeatedly calls for a framework.<br />

That’s why Apple put this data modeling and persistence functionality into a framework<br />

and called it Core Data. It was first introduced in 2005 with Mac OS X (10.4)<br />

Tiger. Core Data is the M in MVC (model-view-controller). It takes care of the complete<br />

model layer, including the relationships among the model objects, and makes it<br />

extremely easy to save an object graph to and load it from disk.<br />

Before we dive into using Core Data, let’s look into the objects that make Core<br />

Data work.<br />

12.1.1 What does Core Data do?<br />

Core Data isn’t just a wrapper for database access. In most cases, however, Core Data<br />

uses a SQLite database to store the data on disk. On Mac OS X, developers usually also<br />

choose SQLite when working with Core Data, but the option to save the data in XML<br />

format is available too, which shows that Core Data isn’t simply a database access<br />

library. To explain what’s going on under the hood, we take a brief look at SQLite<br />

because it’ll make some of the underpinnings easier to understand.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!