How to watch NSMetadataItem for iCloud updates?

Apples sample code suggests observing NSMetadataQueryDidUpdateNotification for detecting changes in iCloud, but it fires several times during an iCloud transaction. Their application processes the files in the "fileListReceived" message every time NSMetadataQueryDidUpdateNotification is launched.

This is not an ideal solution for multi-document applications.

voromax suggests here that the best solution is to monitor NSMetadataItem attribute values.

Can someone explain / show / clarify how this works, or suggest an alternative solution?

+4
source share
1 answer

NSMetadataQuery is suitable for viewing the iCloud file container for files that appear or disappear (i.e. add or delete). Although you are correct that a notification can be triggered often, regardless of whether it is suboptimal or not in your application, it depends on what you do with the returned data. In this example, the entire list is reloaded each time, but if you need to process many documents, which are often updated frequently, you can come up with a more complex approach that works only with changes.

To view changes (contents) of documents in a container (for example, if they are open, and you want to update the user interface with changes), you need to combine the metadata request with the UIDocumentStateChangedNotification observation for each document that interests you in this context. He will tell you when the document was updated, and you can react - update the interface, etc.

I believe the question related to you is talking about using KVO to track the properties of individually returned NSMetadataItems, in particular the download status, to determine exactly when the document finished loading (or loading) from iCloud. This would be useful for displaying a progress bar, for example. This is not a good replacement for UIDocumentStateChangedNotification, but it can be a good addition. To truly have a robust implementation of UIDocument / iCloud, many applications combine all of these methods to give the user a better experience.

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1444023/


All Articles