I'd like to show a progress bar while a UIDocument is being downloaded from iCloud.
I tried keeping a reference to the NSMetadataItem
from the query asking it for its attributes every second, but the key NSMetadataUbiquitousItemPercentDownloadedKey
either does not exist, or it suddenly changes to 100
once the document was downloaded.
Relevant code:
// get meta data item from query
NSNumber *downloadProgress = [self.documentMetadataItem valueForAttribute:NSMetadataUbiquitousItemPercentDownloadedKey];
NSLog(@"download status: %@", downloadProgress);
Instead of polling the NSMetadataItem
for changes, you should just listen for updates from the NSMetadataQuery
and get NSMetadataUbiquitousItemPercentDownloadedKey
once (and update the UI) each time you receive an update.
(I think existing NSMetadataItem objects are not updated as the download progresses, except to transition from downloading to fully downloaded).
Here is a way of listening to NSMetadataQuery updates using KVO:
// To register for KVO updates:
[query addObserver:self
forKeyPath:@"results"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
// check that "object" is your metadata query then:
NSUInteger changeKind = [[change objectForKey:NSKeyValueChangeKindKey] unsignedIntegerValue];
switch (changeKind) {
case NSKeyValueChangeSetting:
// The whole array of results changed, update your model accordingly
// ...
break;
case NSKeyValueChangeReplacement:
// This is where you update your progress bar in most cases
// Enumerate [change objectForKey:NSKeyValueChangeNewKey], this is a list of NSMetadataItems that will have all the desired properties
break;
case NSKeyValueChangeInsertion:
// Handle a new document
// ...
break;
case NSKeyValueChangeRemoval:
// Handle a removed document
// ...
break;
}
}
If you still see the progress not updating, please file a bug with Apple.