Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

18. iCloud Basics > Metadata Queries and the Cloud - Pg. 98

return YES; } - (id) contentsForType:(NSString *)typeName error:(NSError *__autoreleasing *)outError { NSLog(@"Publishing content"); return UIImageJPEGRepresentation(self.image, 0.75f); } @end Get This Recipe's Code To get the code used for this recipe, go to https://github.com/erica/iOS-5-Cookbook, or if you've downloaded the disk image containing all the sample code from this book, go to the folder for Chapter 18 and open the project for this recipe. Metadata Queries and the Cloud Two new metadata scopes, NSMetadataQueryUbiquitousDataScope and NSMeta- dataQueryUbiquitousDocumentsScope , point to the iCloud's Data and Documents folders. You can add an ongoing query to monitor these folders, creating a callback whenever the folder contents change. This allows you to know when new items appear or disappear and update your file listings accordingly. Listing 18-1 offers a pair of meth- ods that demonstrate how to use this technology to query and monitor the folder: The first method, startMonitoringUbiquitousDocumentsFolder , sets up a monitor for the Documents folder scope, listening for update notifications. When these occur, the method uses an informal delegate callback to update a client. The second method, stopMonitoringUbiquitousDocumentsFolder , cancels the notification observation, allowing your code to stop any ongoing monitoring. To search the ubiquitous container outside the Documents folder, use the NSMetadataQueryUbiquitousDataScope search scope instead of NSMetadata- QueryUbiquitousDocumentsScope . You can add both scopes via setSearchScopes: or pass an empty array to remove search scope limitations. In iOS 5, you cannot yet use the NSMetadataQuery class to search sandboxes or other parts of iOS outside of author- ized iCloud container storage. The startMonitoringUbiquitousDocumentsFolder method, shown here, auto- matically extracts filenames (via the NSMetadataItemFSNameKey key) to an array and returns those to an informal delegate. You might want to adapt the method to return the raw NSMetadataItem objects or URLs ( NSMetadataItemURLKey ) instead. Use file updates to refresh data sources but never depend on them to precisely indi- cate an update event. You may receive some callbacks that seem untied to anything other