gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:50:54 27 4
gpt4 key购买 nike

我正在尝试一些代码来使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com