Shashank
Shashank

Reputation: 1753

Block passed as a parameter to MDQuerySetSortComparatorBlock (Spotlight API) is not getting called

I tried to use MDQuerySetSortComparatorBlock method inside the MDQuery framework to sort the query result list. However, the block passed as a parameter to the method doesn't get called at all. Below is a piece of code that I'm using to make a query request. Any help is appreciated.

queryString = [NSString stringWithFormat:@"kMDItemDisplayName == \'*a*\'cd"];
CFStringRef query = (__bridge CFStringRef)(queryString);

queryReference = MDQueryCreate(NULL, query, (__bridge CFArrayRef)([NSArray arrayWithObjects:(id)kMDItemDisplayName, nil]), NULL);

MDQuerySetSortComparatorBlock(queryReference, ^CFComparisonResult(const CFTypeRef *attrs1, const CFTypeRef *attrs2) {

    NSLog(@"Block called");
    return kCFCompareLessThan;

});

MDQuerySetSearchScope(queryReference, (__bridge CFArrayRef)([NSArray arrayWithObjects:(id)kMDQueryScopeComputer, nil]), 0);

MDQueryExecute(queryReference, kMDQueryWantsUpdates);

Upvotes: 4

Views: 212

Answers (1)

Shashank
Shashank

Reputation: 1753

The fourth parameter in the mdquerycreate method has been provided for sorting purpose. We can pass an array of attributes to it to sort the results. If the fourth parameter is not NULL, the sortcomparator block gets called.

Upvotes: 2

Related Questions