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.

Building the PocketTasks application<br />

239<br />

important line here is the call to NSEntityDescription’s insertNewObjectForEntity-<br />

ForName:inManagedObjectContext: method. It creates a new Person managed object<br />

and returns it. You can set its values in the next two lines. Notice, however, that this doesn’t<br />

save anything yet! Only the call to save: on managedObjectContext saves the data.<br />

For some extra fun, comment the line that sets either the first or the last name and<br />

build and run again. Notice in the Console that Core Data refuses to save your data<br />

because you defined that both the first and last names are required.<br />

12.4.5 Fetching Person entities in pure code<br />

Naturally, you want access to the data you’ve saved (otherwise, this whole thing would<br />

be pretty pointless, wouldn’t it?). Listing 12.5 shows the implementation of the dump-<br />

DataToConsole method. And it does just that: fetches all Person entities ordered by<br />

the last name and outputs them to the Console. The ordering is accomplished by an<br />

array of NSSortDescriptors (just one, in this case). An instance of NSSortDescriptor<br />

describes how objects should be ordered by setting the name of the property and,<br />

optionally, a method or block that should be used to compare objects and by specifying<br />

whether the order should be ascending or descending. The method setSort-<br />

Descriptors: of NSFetchRequest takes an array of NSSortDescriptors because you<br />

might want to order by last name and then first name, for example.<br />

Listing 12.5<br />

Dumping all Person entities to the Console in<br />

PocketTasksAppDelegate.m<br />

- (void)dumpDataToConsole {<br />

NSManagedObjectContext *moc = [self managedObjectContext];<br />

NSFetchRequest *request = [[NSFetchRequest alloc] init];<br />

[request setEntity:[NSEntityDescription entityForName:@"Person"<br />

inManagedObjectContext:moc]];<br />

// Tell the request that the people should be sorted by their last name<br />

[request setSortDescriptors:[NSArray arrayWithObject:<br />

[NSSortDescriptor sortDescriptorWithKey:@"lastName"<br />

ascending:YES]]];<br />

NSError *error = nil;<br />

NSArray *people = [moc executeFetchRequest:request error:&error];<br />

[request release];<br />

}<br />

if (error) {<br />

NSLog(@"Error fetching the person entities: %@", error);<br />

} else {<br />

for (NSManagedObject *person in people) {<br />

NSLog(@"Found: %@ %@", [person valueForKey:@"firstName"],<br />

[person valueForKey:@"lastName"]);<br />

}<br />

}<br />

Finally, you just need to change the application:didFinishLaunchingWith-<br />

Options: method to call the dumpDataToConsole method instead of the create-<br />

SampleData method, as in the following listing.

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

Saved successfully!

Ooh no, something went wrong!