[SOLVED] Waiting for NSMetadataQuery to finish?

  • Hi,

    yesterday I had this problem:

    > is it somehow possible to wait for an NSMetadataQuery to first give
    > back some results, and only then continue in the code?
    > Here is my situation: in an NSTextView I want to run a Spotlight
    > query on autocomplete and fill the autocomplete drop-down box with
    > the results of the query.

    I think I have a solution (it seems to work), using CFRunLoops.
    Basically, after starting the query in the NSTextView's delegates
    textView:completions:forPartialWordRange:indexOfSelectedItem:, I
    begin a new run loop. At the end of the call-back funtion listening
    to NSMetadataQueryDidFinishGatheringNotification, I stop this new run
    loop again. Below is the (abbreviated) code.

    Please tell me if that use of CFRunLoop is ok?

    - (NSArray *)textView: (NSTextView *)textView
             completions: (NSArray *)words
      forPartialWordRange: (NSRange)charRange
      indexOfSelectedItem: (int *)index
    // get the current word. That will be the keyword in our query
    NSString *word = [[textView string] substringWithRange: charRange];

    // construct the objects needed to perform the query
    NSMetadataQuery *query = [[NSMetadataQuery alloc] init];

    [[NSNotificationCenter defaultCenter]
                  addObserver: self
        selector: @selector(queryHandler:)
        name: NSMetadataQueryDidFinishGatheringNotification
                     object: query];

    [query startQuery];

            // start a new run loop, efffectively halting at this point

    return [self suggestions];

    - (void)queryHandler: (NSNotification *) inNotification
    NSMetadataQuery *query = [inNotification object];
            // do something with the query

    [self setSuggestions: suggestions];

            // stop the run loop again
    CFRunLoopStop(CFRunLoopGetCurrent ());

    Knud Möller, MA
    +353 - 91 - 495086
    Digital Enterprise Research Institute
      National University of Ireland, Galway
    Institiúid Taighde na Fiontraíochta Digití
      Ollscoil na hÉireann, Gaillimh
  • On Oct 19, 2006, at 5:48 AM, Knud Hinnerk Möller wrote:

    > Please tell me if that use of CFRunLoop is ok?

    Hi Knud,

    Yes, in general this use of a nested run loop is okay... There are a
    couple of subtleties to be aware of though. Foremost, since you're
    nested loop is running in the default run loop mode, you're likely to
    have other run loop sources fire while you're "blocked" in the nested
    loop. This may or may not cause problems, depending on what sources
    are active and how they're handled.

    If this turns out to be an issue, executing the query synchronously
    on a worker thread is another option. It's worth noting that all
    synchronous query execution does is invoke a nested run loop, so
    doing a synchronous MDQueryExecute() on the same thread still has
    potential issues with other run loop sources (in Tiger at least).


    -- Kaelin
previous month october 2006 next month
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today