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.

252CHAPTER 8: Data Management with Core DataListing 8-17. Initializing the Importer#import "APAddressBookImporter.h"#import < AddressBook/AddressBook.h>#import "Person.h"#import "MailingAddress.h"#import "EmailAddress.h"#import "PhoneNumber.h"@implementation APAddressBookImporter{NSManagedObjectContext *_context;}- (id) initWithParentObjectContext: (NSManagedObjectContext *) parent{self = [super init];if ( self == nil )return ( nil );}@end_context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];[_context setParentContext: parent];// because it has a parent, it doesn't need a persistentStoreCoordinatorreturn ( self );You also want to keep hold of the completion block passed into thebeginImportingWithCompletion: method. The best way to handle this is to define a property witha storage type of copy and to use that to store and access the block. This isn’t part of the publicAPI, and properties can only be declared in the public interface or a class extension, so it lookslike you must use the latter. Place the code from Listing 8-18 above your @implementation block.Listing 8-18. The Completion Handler Property@interface APAddressBookImporter ()@property (nonatomic, copy) void (^completionHandler)(NSError *);@endThe syntax may look a little weird, but if you go back to the early chapters and look at thesections on function-pointer and block syntax, you’ll see that it’s actually correct: the type isvoid, and everything that follows it is the name. The braces in there simply denote that it’s acallable block of code.The actual work of importing is done in the background; you accomplish this using dispatch_async(), and everything else you do will take place within that single block. Unfortunately, theAddress Book API expects that anything loaded from a single ABAddressBook instance will beused on the same thread (hm, that sounds… familiar…) so you won’t parallelize this further.In APAddressBookImporter.m, place the code from Listing 8-19 just below your initializer.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!