You can try to use this code to process notifications in background thread:
NSMetadataQuery *query = [NSMetadataQuery new];
[query enableUpdates];
// Subscribe to query updates and process then in background thread
[[NSNotificationCenter defaultCenter] addObserverForName:
NSMetadataQueryDidUpdateNotification
object:nil queue:[NSOperationQueue new]
usingBlock:^(NSNotification __strong *notification)
{
// disable the query while iterating
[query disableUpdates];
for (NSMetadataItem *item in query.results)
{
//do here everything you want with the results
//e.g. get values with [item valueForAttribute:NSMetadataItemFSNameKey];
}
[query enableUpdates];
}];
[query startQuery];
Just for the tip:
[NSOperationQueue mainQueue] // added operations execute on the main thread
[NSOperationQueue new] // iOS 4 and higher - guaranteed to be on the background thread
P.S to process first results you should subscribe to NSMetadataQueryDidFinishGatheringNotification
- it will be posted when the receiver has finished with the initial result-gathering phase of the query and for all subsequent updates you should subscribe to NSMetadataQueryDidUpdateNotification
as in my example because as you know queries have two phases: the initial gathering phase that collects all currently matching results and a second live-update phase.