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 309"error" : {"Code" : 133000,"Domain" : "NSCocoaErrorDomain","UserInfo" : {"LocalizedDescription" : "No object found with identifier 'coredata://1A88B03A-FE45-4E69-8D85-1179B5A1898E/Person/z109'","Affected Stores" : ""}}}Clients and CommandsAs connections arrive from client applications, the XPC service is going to create instances ofa new class, APAddressBookClient, to manage the connection and handle data transfer. Thisclass’s responsibilities include• Ownership of the client connection’s socket, including detecting when it hasbeen disconnected and releasing resources.• Reading data as it arrives and accumulating into single commands.• Passing commands on for processing.• Sending data to the client.To post commands out for processing, it will specify a delegate protocol; this will define methodsto handle incoming commands and to be notified of a client’s disconnection. The object itself willonly provide two methods: an initializer, which requires a delegate object, and a method to senda chunk of data.Create a new <strong>Objective</strong>-C class in the Networker target, an NSObject subclass, and name itAPAddressBookClient. Enter the code from Listing 9-43.Listing 9-43. The APAddressBookClient Interface@class APAddressBookClient;@protocol APAddressBookClientDelegate - (void) client: (APAddressBookClient *) clienthandleMessage: (NSDictionary *) message;- (void) clientDisconnected: (APAddressBookClient *) clientwithError: (NSError *) error;@end@interface APAddressBookClient : NSObject- (id) initWithSocket: (CFSocketNativeHandle) sockdelegate: (id) delegate;- (void) sendData: (NSData *) data;@endThe data transfer here is going to be implemented in a simple asynchronous manner using thedispatch_io API. You could absolutely use NSStream to do the same thing, but despite using ahigher-level language, NSStream actually implements a lower-level API than dispatch_io. This iswww.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!