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 151// sort results by path[query setSortDescriptors: @[[NSSortDescriptor sortDescriptorWithKey:NSMetadataQueryResultContentRelevanceAttribute ascending: YES], [NSSortDescriptorsortDescriptorWithKey: NSMetadataItemPathKey ascending: YES]]];// group results by their type-- this attribute is a Uniform Type Identifier// to display the UTI we'll fetch its description[_query setGroupingAttributes: @[(__bridge id)kMDItemContentType]];Once a query has been created, it can be launched asynchronously by calling its -startQuerymethod. To receive information about its progress and completion, however, you need to registerfor the notifications described in Table 5-9.Table 5-9. NSMetadataQuery Status NotificationsNotification NameNSMetadataQueryDidStartGatheringNotificationNSMetadataQueryGatheringProgressNotificationNSMetadataQueryDidFinishGatheringNotificationNSMetadataQueryDidUpdateNotificationDescriptionThe metadata query has begun to acquire detailsof matching items.Sent periodically as more results are gathered butbefore the complete list is ready.Sent to indicate that the initial search hascompleted.Sent after initial gathering is complete as newitems appear that match the query, or previouslymatched items no longer do so.The query itself operates in two phases. The first phase, gathering, obtains a list of matchingitems and returns them. The second phase is optional, and is called live update. At this pointyou’ll receive notifications whenever the contents of the gathered list change based on updatesto files or the creation of new ones. If you’re only doing a one-time search, you can skip thelive update phase by sending -stopQuery to your NSMetadataQuery object after processing theresults from the gathering phase.Once you receive the gathering-complete notification, you can iterate across all available items.To ensure that live updates don’t interrupt this, call -disableUpdates and -enableUpdates eitherside of the operation. In Listing 5-37, you can see this in action: a matched list of items isprinted out in groups, with each group’s type printed in its localized display form. This sample isavailable in a full Xcode project online as PerformSpotlightSearch.Listing 5-37. Listing the Results of an NSMetadataQuery- (void) gatherComplete{// pause live updating[_query disableUpdates];// iterate through the groupsfor ( NSMetadataQueryResultGroup * group in [_query groupedResults] )www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!