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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 5: Using the Filesystem 145}}}];// read the file to ensure we have the latest dataNSString * content = [[NSString alloc] initWithContentsOfURL: newReadingURLencoding: NSUTF8StringEncoding error: NULL];@synchronized(self){[self importEntriesFromString: content];// write out the new dataNSMutableString * output = [NSMutableString new];. . .[output writeToURL: newWritingURL atomically: YESencoding: NSUTF8StringEncoding error: NULL];The -coordinateReadingItemAtURL:options:writingItemAtURL:options:error:byAccessor:method is primarily designed to assist with applications that are, for example, copying contentfrom one item to another. In this case, however, it helps you to avoid any race conditions betweenthis instance and any others. Note also that the coordinator provides you with new URLs for bothreading and writing, as either item could potentially have been moved or renamed in the interim.As in the FolderInfoPresenter class, the -presentedItemURL and -presentedItemOperationQueuemethods simply return the URL of the info file and the operation queue provided duringinitialization respectively. It gets more interesting when working with readers and writers in otherprocesses: you mark the instance as suspended to prevent any reads or writes going through,but you keep track of any such operations while suspended and perform them when youreacquire the item. Listing 5-31 shows this in action.Listing 5-31. Synchronizing Access to the File- (void) relinquishPresentedItemToReader: (void (^)(void (^reacquirer)(void))) reader{fprintf(stdout, "Relinquishing file to reader.\n");_suspended = YES;reader(^{_suspended = NO;if ( _deferredRead ){_deferredRead = NO;[self loadFile];}if ( _deferredWrite ){// there was a request to write the file while we were suspended// fire off the operation now_deferredWrite = NO;[self writeFile];}});}www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!