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 143your view of the item. Since you don’t look at any attributes of the presented folder itself, youonly have to remove the suspension at this point.The next method deals with the deletion of the presented folder. In this case, you print amessage to the console and stop the main run-loop, causing the application to exit. Next comesa handler for the case when your presented folder is either moved or renamed; here you take theopportunity to update your _folderURL instance variable to the new value.When new items are added inside your folder, the file coordination system will inform you. Yourreaction is fairly simple: rather than iterate through the folder’s contents again to build up thetotals from scratch, you can simply look at the size of the new item and add it on its own. Thecall should still be made using file coordination, however, so use an NSFileCoordinator instanceto do so.Note Remember that NSFilePresenter notification messages are sent via the file presenter’squeue, so the file coordination system is not waiting for these methods to return. This means thatyou can safely call NSFileCoordination APIs directly from within any of these notificationmethods without fear of causing a deadlock.The implementation of the above logic can be seen in Listing 5-28.Listing 5-28. Handling the Appearance of a New Sub-Item- (void) presentedSubitemDidAppearAtURL: (NSURL *) url{// handled very simply: add the new item's sizes to our totals// Apple recommends that this occur via NSFileCoordinator coordinateReading. . .NSFileCoordinator * c = [[NSFileCoordinator alloc] initWithFilePresenter: self];[c coordinateReadingItemAtURL: url options: NSFileCoordinatorReadingWithoutChanges error:NULL byAccessor: ^(NSURL *newURL) {NSDictionary * attrs = [url resourceValuesForKeys: @[NSURLFileSizeKey,NSURLTotalFileAllocatedSizeKey]error: NULL];_totalFileSize += [[attrs objectForKey: NSURLFileSizeKey] unsignedIntegerValue];_totalAllocatedSize += [[attrs objectForKey: NSURLTotalFileAllocatedSizeKey]unsignedIntegerValue];}];}// send the new details to the info file[self updateInfoFile];The method of dealing with a deleted sub-item is very similar; instead of adding the details, youremove them. However, since you are being messaged prior to the sub-items actual deletion,you are provided a block to call when you’re done. The implementation of this method looksalmost identical to that in Listing 5-28, except that you subtract the file sizes from your totalsand you call the provided completionHandler block before returning.www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!