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

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

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

CHAPTER 9: Writing an Application 295if ( identifier == nil )continue;}NSMutableDictionary * personInfo = [NSMutableDictionary new];personInfo[APRemoteAddressBookCommandPersonIDKey] = identifier;if ( person.firstName != nil )personInfo[@"firstName"] = person.firstName;if ( person.lastName != nil )personInfo[@"lastName"] = person.lastName;[result addObject: personInfo];if ( [result count] == 0 )result = nil;}dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{reply(result, error);});}];Address DataYour address objects are all vended through similar methods: -ForPersonWithIdentifier:reply:. These all return every attribute of their respective entities and all are fetchedbased on their relationship with a single Person entity. This makes them a perfect target for somegeneralization. You define a single method taking an entity name, an identifier string, and anXPC reply block. This method will then unpack the object ID from the identifier, sending an errorto the reply block upon failure. Then an NSEntityDescription is obtained using the entity nameprovided, and an NSFetchRequest made using that, its predicate set to limit the results to onlythose objects whose person relationship refers to the Person identified by the object ID.The request itself is set to return results in the form of an NSDictionary per object, rather thanthe usual NSManagedObject or NSManagedObjectID instances. These dictionaries contain thevalues for each attribute specified through the fetch request. Since you want to return allattributes, you can ask the entity itself for a list of their names to pass to the fetch request’s-setPropertiesToFetch: method. This is accomplished by asking it to return a dictionary ofall attribute descriptions indexed by their names, and asking that dictionary for all its keys—[[entity attributesByName] allKeys].The results of the fetch request are already in the exact format you want to send back to thereply block: an array of dictionaries. Having performed the fetch request, the results and errorcan be passed to the reply immediately without any further work. Having implemented thismethod, each of the three address-vending methods can be implemented in a single line; theonly difference between them is the name of the entity passed into the generic method.The complete code for this method, along with one of the address methods, is shown inListing 9-28.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!