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.

138CHAPTER 5: Using the Filesystemfile coordination system keeps track of this fact and will refetch the URL after the presentercompletes any coordinated file operations, in case the item was just created.The presentedItemOperationQueue value, on the other hand, cannot be nil. All messages sentto the presenter will take place on this queue. In very simple cases you might be able to return[NSOperationQueue mainQueue] here, but ordinarily this would be an invitation to deadlocks.The methods of the NSFilePresenter protocol fall into three groups.• Coordinating Access: These methods enable the presenter to ensure thatreaders get access to the latest updates by writing out any unsaved contentand provide notifications so a presenter can avoid interrupting any otherprocesses’ reads and writes. Your presenter will also be notified if its contentfile is renamed, deleted, or moved to a new location.• File Changes: Any modification to the file’s contents or attributes, whetherthat modification was done through file coordination or otherwise, will triggera message to the presenter. Your presenter can then check to ensure that ithas an up-to-date copy of the file’s data. On OS X 10.7 and iOS 5, you mayalso be notified of versioned changes to the file, whether a new version wasadded, removed, or a conflict was resolved.• Folder Changes: When your file presenter is presenting an entire folder,you will be informed of any changes to the contents of that folder and itssubfolders. You will be able to react to pending deletion of a sub-item, andyou’ll be notified of any new sub-items appearing, any move or renameoperations, and any versioning information similar to that provided forpresenters of individual files.Trying It OutTo properly illustrate the use and capabilities of file coordination, I’ll walk you through the creationof a fairly contrived example: a command-line app that monitors the file size and allocatedsize of the contents of a folder and writes this data to a nearby log file. It will update the logfile using two separate file presenters, which will have to coordinate their updates to the filebetween themselves. As a result, both file presenters will wind up working with the same data,with potentially some different unsaved content at any time. Each time something in the folderchanges, one of the log file’s presenters will be informed of the new totals and will record them.Whenever another presenter (such as the Finder or QuickLook) wishes to read from an item, thepresenters will record any unsaved changes, being sure to load any new content from the file first.The full source code is available as an Xcode project named FileCoordinationExample online.In the book, I will skip over some details and focus on the implementation of the major filecoordination API elements.Watching a FolderThe first task is to create an object that will watch an individual folder and keep track of its size.You can use the FolderInfoPresenter class for this purpose, which has the very simple interfaceseen in Listing 5-23.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!