5

Apples code example suggests observing NSMetadataQueryDidUpdateNotification for detecting changes in iCloud, but this fires multiple times per iCloud transaction. Their app processes the files in the "fileListReceived" message every time NSMetadataQueryDidUpdateNotification fires.

This is not an ideal solution for applications with many documents.

voromax suggests here that a better solution is to observe the attribute values of NSMetadataItem.

Can anyone please explain/show/elaborate on how this would work, or offer an alternative solution?

Community
  • 1
  • 1
Tom Harris
  • 173
  • 1
  • 8

1 Answers1

3

NSMetadataQuery is good for watching the iCloud file container for files that appear or disappear (i.e. add or remove). While you're correct in that the notification can fire often, whether it's sub-optimal or not in your app depends on what you do with the data returned. The example reloads the entire list each time but if you need to handle a lot of documents that are potentially updated often, you could come up with a more sophisticated approach that only operates on the changes.

For watching changes (to the contents) of documents in the container (for instance if they are open and you want to update the UI with changes), you need to combine the metadata query with observing UIDocumentStateChangedNotification for each document you care about in this context. It will tell you when the document has been updated and you can react - update the UI, etc...

I believe the question you linked is talking about using KVO to track the properties of individually returned NSMetadataItems, specifically the download status, to determine exactly when a document has finished downloading (or uploading) from iCloud. This would be useful for displaying a progress bar, for example. This is not a good substitute for UIDocumentStateChangedNotification but it could be a good compliment. To really have a robust UIDocument/iCloud implementation, many apps combine all of these methods to give the user the best experience.

Hope that helps.

Hunter
  • 4,343
  • 5
  • 42
  • 44