为什么要快速连续多次调用NSMetadataQueryDidUpdateNotification? [英] Why is NSMetadataQueryDidUpdateNotification being called several times in quick succession?
问题描述
为了监控我的iCloud容器中的文件更改,我已注册
In order to monitor file changes in my iCloud container, I've registered for
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(processiCloudUpdates:)
name:NSMetadataQueryDidUpdateNotification
object:nil];
理想情况下,每当我收到更新消息时,我想打开每个我的UIDocs并检查设置是否已更改。如果 NSMetadataQueryDidUpdateNotification
只被调用一次,那就没问题了。但是,它被多次触发(我没有忘记 removeObserver
等并且有几个实例正在运行),因此每次加载所有文件都非常不切实际。更新通知被触发的时间(如果我有5个文件并且这被调用4次,我将快速连续打开20个UIDoc)。例如,如果我更新一个文件(并且在我无处不在的容器中只有一个文件),就会发生这种情况:
Ideally, whenever I receive the message that updates have been made, I'd like to open each of my UIDocs and check if settings have been changed. This would be fine, if NSMetadataQueryDidUpdateNotification
was only called once. However, it is being fired several times (I did not forget to removeObserver
etc. and have several instances running), and it would thus make it highly impractical to load all files every time the update notification is being fired (if I had 5 files and this is being called 4 times, I'd open 20 UIDocs in quick succession). For instance, this happens if I update one file (and only have one file in my ubiquitous container):
2012-07-05 10:51:39.565 Meernotes[7842:707] ... NSMetadataQuery update
2012-07-05 10:51:42.468 Meernotes[7842:707] ... NSMetadataQuery update
2012-07-05 10:51:45.216 Meernotes[7842:707] ... NSMetadataQuery update
2012-07-05 10:51:47.036 Meernotes[7842:707] ... NSMetadataQuery update
还有其他方法可以确定文件何时发生变化? NSFileVersion也没有多大帮助。
Is there any other way to determine when a file has changed? NSFileVersion did not help much either.
推荐答案
NSMetadataQueryDidUpdateNotification
每次发布时都会出现问题 NSMetadataQuery
结果更新中的任何 NSMetadataItem
的一些属性。它包括部分上传或下载进度。因此,在此通知处理期间尝试打开 UIDocument
实例是错误的。
NSMetadataQueryDidUpdateNotification
issues each time when some of attributes of any NSMetadataItem
in NSMetadataQuery
results update. It includes the the partial upload or download progress. So it is wrong to try to open UIDocument
instance during this notification handling.
相反,您可以通过观察 NSMetadataItem
的属性值来确定文档下载的时刻,然后对其进行排队打开。
Instead you could determine the moment when the document did download by observing the attribute values of NSMetadataItem
and then queue it to be opened.
不要忘记处理 NSMetadataQueryDidFinishGatheringNotification
。它是在接收者完成初始结果时发布的 - 查询的收集阶段
Do not forget to handle NSMetadataQueryDidFinishGatheringNotification
too. It is posted when the receiver has finished with the initial result - gathering phase of the query
这篇关于为什么要快速连续多次调用NSMetadataQueryDidUpdateNotification?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!