01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Core Data objects<br />

231<br />

In Xcode you can create a new project configured to use Core Data as the storage<br />

mechanism, which involves importing the CoreData.framework. Projects configured<br />

to use Core Data also create the Xcode data model that’s used to define what your<br />

data looks like, also known as its schema.<br />

12.2 Core Data objects<br />

The functionality of Core Data is achieved through four distinct objects that compose<br />

what is known as the Core Data stack. These are the four major objects that make up Core<br />

Data’s functionality. We investigate the purpose of each in the next several sections. The<br />

singular point of access is through the managed object context, discussed next, so if<br />

you’re less concerned with the nuts and bolts of the system, you don’t need to be too<br />

concerned about the persistent store coordinator and the persistent object store.<br />

12.2.1 Managed object context<br />

NSManagedObjectContext is the object that you work with to add, access, modify, or<br />

delete objects. When you access a single element out of a managed object context, an<br />

NSManagedObject or a subclass of it is returned. In the Person example, a returned<br />

NSManagedObject could be a Person object. You aren’t required to create subclasses<br />

for every entity, though. You can also work directly with NSManagedObject instances, as<br />

you’ll see in this chapter’s demo application.<br />

When you’re working with an NSManagedObject retrieved from your managed<br />

object context, a good way to think of how the logic works is to compare it to a text<br />

file: if you open a text file, change a whole bunch of the text, add some, remove some,<br />

nothing matters unless and until you save it. So when you retrieve an NSManaged-<br />

Object from your managed object context, the changes you make to it are applied to<br />

the database only when you tell the managed object context to save it. We get into the<br />

details of these activities later in this chapter, but the important point is to know that<br />

by using the managed object context, you can add, retrieve, and save NSManaged-<br />

Objects or your custom subclasses.<br />

12.2.2 Persistent store coordinator<br />

In the stack, you work exclusively with the managed object context, and while a complex<br />

application may have several managed object contexts, all Core Data–based projects<br />

have only a single store coordinator. The persistent store coordinator sits between<br />

a managed object context and a persistent object store. You can think of it as the key<br />

holder to the databases. The managed object context must ask the persistent object<br />

coordinator if the requested access modification or deletion is allowed. It’s important<br />

that a storage controller contains lots of checks to validate actions by its user, which is<br />

why you use only a single persistent store coordinator. If more than one coordinator<br />

exists, conflicting access to the database may occur. The persistent store coordinator<br />

manages all of the logic necessary to safely access and modify database tables.

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

Saved successfully!

Ooh no, something went wrong!