Why is NSMetadataQueryDidUpdateNotification being called several times in quick succession?

耗尽温柔 提交于 2019-12-19 10:05:31

问题


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];

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

Is there any other way to determine when a file has changed? NSFileVersion did not help much either.


回答1:


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.

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.

Do not forget to handle NSMetadataQueryDidFinishGatheringNotification too. It is posted when the receiver has finished with the initial result - gathering phase of the query



来源:https://stackoverflow.com/questions/11341992/why-is-nsmetadataquerydidupdatenotification-being-called-several-times-in-quick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!