Indoor
Indoor

Reputation: 561

UIDocument openWithCompletionHandler: forces NSMetadataQuery to update

I’m trying to use iCloud and an app is listening to NSMetadataQuery updates subscribing to NSMetadataQueryDidFinishGatheringNotification.

When the app receives notification I read some iCloud UIDocument files using openWithCompletionHandler: but that call causes NSMetadataQuery to notify me of updates again and so app gets in an endless cycle update > read > update > read > …

Should openWithCompletionHandler: behave this way? What can I do to prevent it from such a cycle?

Upvotes: 1

Views: 290

Answers (2)

Nick Entin
Nick Entin

Reputation: 1087

I faced this issue too, and it seems, like the answer to your question

Should openWithCompletionHandler: behave this way?

is "Yes". I can just assume it, but it sounds reasonable, that when you do openWithCompletionHandler: the block is called, when all the data is loaded. However with NSMetadataQueryDidUpdateNotification you can trace downloading progress (e.g. percentage of data loading before your openWithHandler block is called).

My solution so far is:

  • watch for notification updates of NSMetadataUbiquitousItemDownloadingStatusCurrent
  • disableUpdates when I suppose to open the document
  • enableUpdates for the query inside of block of the document opening

Due to the fact, that my query is watching a single file, I'm not afraid to lose notifications, which may be skipped during one document opening. It might be not the case if there are several documents watched, but I don't know how to solve it better

Upvotes: 1

nmh
nmh

Reputation: 2503

Should openWithCompletionHandler: behave this way?

It is not correct. NSMetadataQuery notify updates. It causes by:

  • (NSMetadataQueryDidUpdateNotification) , it means that iCloud update data (uploading file from sandbox to iCloud or downloading file from iCloud to sandbox)

  • When you call [_query startQuery], it call NSMetadataQueryDidFinishGatheringNotification just one to notify data in iCloud. After that, when data has update NSMetadataQueryDidUpdateNotification

I think this circle is correct because when you have new data from iCloud, you should update data in your screen.

Hope this information helps you.

Upvotes: 0

Related Questions