13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

292CHAPTER 9: Writing an Application}}];{}dispatch_async(dispatch_get_main_queue(), ^{[NSApp presentError: error];});As you can see, the first action is to ensure that the XPC connection has been initialized and abrowser object obtained. The -vendAddressBook:errorHandler: is then called, passing the appdelegate as the address book proxy object. If an error occurs, the supplied error handler blockwill be called, affording the delegate an opportunity to present an alert dialog to the user with thepertinent details.Providing DataThe app delegate provides access to its data by implementing the methods of the APAddressBookprotocol, described in APAddressBookInterface.h. Also in this file are the declarations for thekeys used in the command dictionary. While you won’t need to create those dictionaries here,you’ll want to use one of the keys to add an identifier object to the details of each person. At thetop of your app delegate’s implementation file, add the code from Listing 9-25.Listing 9-25. The Person Identifier KeyNSString * const APRemoteAddressBookCommandPersonIDKey = @"personID";With this symbol properly defined, you can start implementing the data methods themselves,starting with -allPeople:. This will send back a simple NSArray instance containing anNSDictionary instance describing each Person object in the data store. The dictionaries willcontain every attribute from the Person objects along with a unique identifier in the form ofan NSString. This identifier will be passed into the other three methods of the APAddressBookprotocol to identify the object from which further details are requested.Since you’re using Core Data, you have a handy unique identifier all ready and waiting: eachobject comes with an NSManagedObjectID that identifies its persistent store and any informationnecessary to locate the object within it. What’s more, managed object IDs have a handy meansof externalizing their data by producing a uniform resource identifier (URI) representation of theirdata. For items in an SQLite store, this is commonly of the form cdata://StoreUUID/EntityName/RowNumber. The URI is an instance of NSURL and provides a string representation of itself throughits -absoluteString method. This also provides an easy way to get an NSManagedObjectID backagain: a new NSURL can be created using the string, and an NSPersistentStoreCoordinator canuse that URL to generate a proper NSManagedObjectID instance.This can be encapsulated into the methods from Listing 9-26, which you can add to your appdelegate implementation. Note that the -identifierForObject:error: method also attempts toensure that no temporary object IDs are used; if an object has a temporary ID (because it hasn’tyet been written to a persistent store), then the context is asked to assign it to one and generatea permanent object ID.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!