iosicloudnsmetadataquery

Why is NSMetadataQueryDidUpdateNotification being called several times in quick succession?


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.


Solution

  • 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