c++ - NSMetaDataQuery:未收到更新或收集完成的通知

标签 c++ macos spotlight nsmetadataquery

我正在尝试一些代码来使用 NSMetaDataQuery 获取基于应用程序路径 在捆绑标识符上。我正在关注在 apple dev 中找到的 hte 示例代码 站点:静态 Spotlight 搜索实现。

我为它写了以下文件

//AppPath.h

void GetAppPath();

@interface SearchQuery: NSObject 
{

}

@property (copy) NSMetaDataQuery *metaData;

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

定义如下:

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

我在 main 中调用 GetAppPAth 方法。我已经添加了必要的头文件。代码编译并运行,但我没有收到任何通知给两个观察者。 我在两个方法中设置了断点,queryDidUpdateinitalGatherComplete。但他们永远不会被击中。我以为失败是因为我的主要代码不是 等待搜索完成。但是即使我在 main 中等待了一些,它也没有用。我还尝试了以下问题中的代码:Not quite understanding NSMetadataQuery 但它以无限的 while 循环结束。

最佳答案

从您发布的代码示例来看,SearchQuery 似乎没有被任何东西保留,因此它会立即被释放。

关于c++ - NSMetaDataQuery:未收到更新或收集完成的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23463857/

相关文章:

java - 无法在 OSX 上启动 Eclipse(版本不适合)

macos - 在 Mac 中通过网络访问 postgresql 服务器

macos - OSX 建议在已安装的驱动器中搜索/查找文件夹

macos - Spotlight 可以索引 MacFUSE 文件系统吗?

C++ 将 'this' 指针传递给基类然后将其存储为子类的另一个基类的预期行为是什么

c++ - C++ 类是否解决了函数覆盖问题?

c++ - STL vector 和线程安全

c++ - 无法将成员变量设置为对象引用

css - 使用 xampp 在 mac os x 上加载 css 和 jquery 文件时出现错误 403

cocoa - 如何让 Spotlight 属性显示在获取信息窗口中?