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.

254CHAPTER 8: Data Management with Core DataListing 18-21. Importing Other Attributes and Saving the Context// email addresses[self importEmailsFromABPerson: abPerson toMine: myPerson];// phone numbers[self importPhonesFromABPerson: abPerson toMine: myPerson];// mailing addresses[self importAddressesFromABPerson: abPerson toMine: myPerson];// now save the contents of the context and/or clean up, in a// synchronized fashion[_context performBlockAndWait: ^{// if the person is valid, save it. Otherwise, reset the context.if ( [myPerson validateForUpdate: NULL] == NO ){[_context reset];}else{NSError * error = nil;if ( [_context save: &error] == NO ){// zap this record and its related objects[_context deleteObject: myPerson];}}}];You’re being especially proactive here. You send a -validateForUpdate: message to theperson object before attempting to save to break out validation errors from anything else thatmight prevent a successful save. Partly this is for reasons of intense paranoi—er, safety. Partlyit’s because, since this data has been copied directly from an external source that doesn’tnecessarily have the same validation criteria, it’s useful to personally make sure everythingchecks out nicely in that regard.If the validation fails, you will discard all unsaved changes by sending the -reset message toyour managed object context. This will dump anything it has in its caches and make it refer backto the persistent store (or in this case its parent context) for any and all data.Now for your address entities—these are broken out into their own importer functions. In theinterest of brevity, we’ll show one in Listing 8-22, which should provide enough information foryou to write the others on your own. If you get stuck, the full implementation is in the Core DataContacts sample project.Listing 8-22. Importing E-mail Addresses- (void)importEmailsFromABPerson:(ABPerson *)abPerson toMine:(Person *)myPerson{NSEntityDescription *entity = [NSEntityDescription entityForName:@"EmailAddress"inManagedObjectContext:_context];ABMultiValue * abEmails = [abPerson valueForProperty: kABEmailProperty];www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!