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.

324CHAPTER 9: Writing an ApplicationNSOperationQueue. The input stream is scheduled on the main run loop and the class is set asits delegate; the output stream is handled separately on a per-operation basis. Then the streamsare opened to complete initialization. The code can be seen in Listing 9-63.Listing 9-63. Initializing the Remote Address Book- (id) initWithResolvedService: (NSNetService *) resolvedServicedelegate: (id) delegate{self = [super init];if ( self == nil )return ( nil );if ( [resolvedService getInputStream: &_inputStreamoutputStream: &_outputStream] == NO )return ( nil );_delegate = delegate;_inputSize = NSNotFound;_inputMessageData = [NSMutableData new];_networkQ = [NSOperationQueue new];[_networkQ setMaxConcurrentOperationCount: 1];[_inputStream setDelegate: self];[_inputStream scheduleInRunLoop: [NSRunLoop mainRunLoop] forMode: NSRunLoopCommonModes];// NOT scheduling the output stream here: it's going to be scheduled only upon// need while a single operation is sending data and needs to wait for output// buffer space to become available.[_inputStream open];[_outputStream open];_replyHandlersByUUID = [NSMutableDictionary new];}return ( self );When the address book is deallocated, the connections get torn down. When using ARC, the-dealloc method can usually be left out and everything will work, but we’ve chosen to use onehere for illustrative purposes in Listing 9-64.Listing 9-64. Remote Address Book Deallocation- (void) dealloc{NSLog(@"Remote address book deallocating: %@", self);[_networkQ cancelAllOperations];if ( [_inputStream streamStatus] != NSStreamStatusClosed )www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!