Use CFRunLoopRun to synchronize NSMetadataQuery


May 12, 2008


No Comment

– (NSArray *)completionsForPartialWordRange: (NSRange)charRange indexOfSelectedItem: (int *)index


// get the current word. That will be the keyword in our query

NSString *word = [[self string] substringWithRange: charRange];

// construct the query string from this word:

NSString *queryString = @”someQueryString”;

// construct the objects needed to perform the query

NSMetadataQuery *query = [[NSMetadataQuery alloc] init];

NSPredicate *pred = [NSPredicate predicateWithFormat: queryString];

[query setPredicate: pred];

// register the call-back

[[NSNotificationCenter defaultCenter]

addObserver: self

selector: @selector(queryHandler:)

name: NSMetadataQueryDidFinishGatheringNotification

object: query];

[query startQuery];

//start a new run loop


// after the new loop is finished, continue by returning the new suggestions:

return [self suggestions];


– (void)queryHandler: (NSNotification *) inNotification


NSMetadataQuery *query = [inNotification object];

NSArray *suggestions = // create the suggestions array from the query result

[self setSuggestions: suggestions];

// stop the new run loop

CFRunLoopStop(CFRunLoopGetCurrent ());


This code was just found on:

Seems to be that the guy also added it to his blog:

The solution looks not very error proved but might someon help…

Related Posts

iPhone OS: Design a nice InApp Purchase View

To get up a nice InAppPurchase View for Appleā€™s AppStore, we first need some good looking buttons: There are also some tweaks for the UITableView you might consider using: more information upcoming…

Read More

iPhone OS: Setup an additional Security Layer

As you might already know the iPhone is itself not that secure that people might think (news is german): How do you prevent intrusion of your customers data? As a really pragmatic approach, we can just set up additional encryption for our application data: So if you use for example core […]

Read More

Leave a Reply


  • An error has occurred, which probably means the feed is down. Try again later.