1

I am trying out some code to use NSMetaDataQuery to get an application path based on bundle Identifier. I was following hte sample code found in the apple dev site: Static Spotlight search implementation.

I wrote the following files for it

//AppPath.h

void GetAppPath();

@interface SearchQuery: NSObject 
{

}

@property (copy) NSMetaDataQuery *metaData;

-(void) initiateSearch;
-(void) queryDidUpdate:sender;
-(void) initalGatherComplete:sender;
@end

Definitions where as follows:

void GetAppPath()
{
    SearchQuery *query = [[SearchQuery alloc] init];
    [query initiateSearch];
}

@Implementation SearchQuery

// Initialize Search Method
- (void)initiateSearch
{
    // Create the metadata query instance. The metadataSearch @property is
    // declared as retain
    self.metadataSearch=[[[NSMetadataQuery alloc] init] autorelease];

    // Register the notifications for batch and completion updates
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(queryDidUpdate:)
                                                 name:NSMetadataQueryDidUpdateNotification
                                               object:self.metadataSearch];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(initalGatherComplete:)
                                                 name:NSMetadataQueryDidFinishGatheringNotification
                                               object:self.metadataSearch];

    // Configure the search predicate to find all application with the given 
    //Bundle Id
    NSPredicate *searchPredicate;
    searchPredicate=[NSPredicate predicateWithFormat:@"NSApplicationBundleIdentifier == 'com.myapp.app'"];
    [self.metadataSearch setPredicate:searchPredicate];

    // Begin the asynchronous query
    [self.metadataSearch startQuery];

}

// Method invoked when notifications of content batches have been received
- (void)queryDidUpdate:sender;
{
    NSLog(@"A data batch has been received");
}


// Method invoked when the initial query gathering is completed
- (void)initalGatherComplete:sender;
{
    // Stop the query, the single pass is completed.
    [self.metadataSearch stopQuery];

    // Process the content. 
    NSUInteger i=0;
    for (i=0; i < [self.metadataSearch resultCount]; i++) {
        //Do Something with the result
    }

    // Remove the notifications to clean up after ourselves.
    // Also release the metadataQuery.
    // When the Query is removed the query results are also lost.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:NSMetadataQueryDidUpdateNotification
                                                  object:self.metadataSearch];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:NSMetadataQueryDidFinishGatheringNotification
                                                  object:self.metadataSearch];
    self.metadataSearch=nil;
}

@end

I am calling the GetAppPAth method in main. I had added the necessary header files. The code compiles and run, but I don't get any notification to the two observers. I had put breakpoints in the two methods, queryDidUpdate & initalGatherComplete. But they are never hit. I thought the failure was because my main code was not waiting for the search to complete. But it was not working even when I had put some wait in main. I had also tried the code in the following question: Not quite understanding NSMetadataQuery But it ended in an infinite while loop.

Community
  • 1
  • 1
sajas
  • 1,599
  • 1
  • 17
  • 39

1 Answers1

1

From the code sample you posted, it looks like the SearchQuery isn't being retained by anything, so it's immediately being deallocated.

garrettm
  • 71
  • 6