Question

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);
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top