4

Apple的代码示例建议观察NSMetadataQueryDidUpdateNotification以检测 iCloud 中的更改,但这会在每个 iCloud 事务中触发多次。每次 NSMetadataQueryDidUpdateNotification 触发时,他们的应用都会处理“fileListReceived”消息中的文件。

对于具有许多文档的应用程序,这不是一个理想的解决方案。

voromax在这里建议更好的解决方案是观察 NSMetadataItem 的属性值。

谁能解释/展示/详细说明这将如何工作,或提供替代解决方案?

4

1 回答 1

2

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.

于 2012-11-11T20:08:51.490 回答