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 8: Data Management with Core Data 255for ( NSUInteger i = 0, max = [abEmails count]; i < max; i++ ){NSString * label = [abEmails labelAtIndex: i];NSString * email = [abEmails valueAtIndex: i];// skip any weird entries which won't fit with our model validationif ( label == nil || email == nil )continue;EmailAddress * e = [[EmailAddress alloc] initWithEntity: entityinsertIntoManagedObjectContext: _context];e.label = ABLocalizedPropertyOrLabel(label);e.email = email;// rather than call the 'add to relationship' methods on myPerson,// we'll just set the to-one relationship on the email, which// does the same thinge.person = myPerson;}}// ensure it's valid -- if not, delete itif ( [e validateForUpdate: NULL] == NO ){[_context performBlockAndWait: ^{[_context deleteObject: e];}];continue;}Lastly, return to APAppDelegate.m and create the -importAddressBookData methodreferenced earlier. You need to create a new APAddressBookImporter object and call its-beginImportingWithCompletion: method, handling any errors reported as appropriate. You alsoneed to save the object context at this point—remember that the importer’s child context onlypushed data up to its parent when it saved; it didn’t actually cause the data to be written out topersistent storage. The complete method can be seen in Listing 8-23.Listing 8-23. Running the Importer- (void)importAddressBookData{APAddressBookImporter * importer = [[APAddressBookImporter alloc]initWithParentObjectContext: self.managedObjectContext];[importer beginImportingWithCompletion: ^(NSError *error) {if ( error ! = nil ){[NSApp presentError: error];}if ( [self.managedObjectContext hasChanges] ){[self.managedObjectContext performBlock: ^{NSError * saveError = nil;www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!