NSMetaDataQuery从不使用NSMetadataQueryDidFinishGatheringNotification回调

时间:2012-11-21 19:57:39

标签: objective-c ios icloud nsnotificationcenter nsmetadataquery

对于我正在编写的iCloud插件,我将iCloud管理器类订阅给这些iCloud NSMetaDataQuery观察者:

// Add a predicate for finding the documents
NSString* filePattern = [NSString stringWithFormat:@"*.%@", @"*"];

self.metadataQuery = [[NSMetadataQuery alloc] init];

// Before starting to query, it is required to set the search scope.
arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

// It is also required to set a search predicate.
[self.metadataQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, filePattern]];

// Listen for the second phase live-updating
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(queryDidReceiveNotification:) name:NSMetadataQueryDidUpdateNotification object:nil];

// Listen for the first phase gathering
[[NSNotificationCenter defaultCenter] addObserver:self
                                      selector:@selector(queryIsDoneGathering:) name:NSMetadataQueryDidFinishGatheringNotification
                                           object:nil];

[self.metadataQuery startQuery];

问题是这些选择器实际上都没有被回调,甚至没有一次,我特别需要 NSMetaDataQueryDidUpdateNotification 来跟踪云中文件的上传/下载进度。

奇怪的是,前几天我有这个工作,但不知怎的,它只是停止了工作,我已经盲目地试图弄明白问题究竟是什么。通过订阅 NSMetadataQueryDidStartGatheringNotification 我可以看到它确实启动了,但它就像它永远不会完成。这很奇怪。

我想知道是否有人知道上述代码有什么问题?或者我可以在哪里寻找问题。

感谢您的时间:)

2 个答案:

答案 0 :(得分:16)

确保在主线程中启动NSMetadataQuery。那时候没有记录这个要求。

答案 1 :(得分:1)

这是挖掘的秘诀。我不知道你现在是否放弃了,但最后,我可能会帮忙。

事实证明,对于某些(所有?)C ++应用程序配置,有一个消息泵不会自动运行。在我的应用程序中,在我的[m_query startQuery]调用之后放置了这样的循环后,我终于开始获得预期的回调:
// make it look syncronous for now while( m_state != finished ) { Check_For_Interruption(); [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:timeslice]]; //<--**this line is the key** }
设置回调以正确更新m_state变量。

我的示例只是阻塞,使用它自己的线程时间片来运行循环(除非被达到超时中断),但这也可以以异步方式设置。

对于那些过度支持传统支持的人来说,另一件需要注意的事情是:
此方法确实导致应用程序在OS X 10.9及更早版本上开始泄漏机器端口。但是,没有看到任何比这更新的问题。

相关问题