从iCloud下载文件时显示进度百分比?

时间:2014-03-19 12:36:39

标签: ios objective-c icloud nsmetadataquery

我想在从iCloud下载UIDocument时显示进度条。

我尝试从查询中保留对NSMetadataItem的引用,每秒都要求它提供其属性,但是密钥NSMetadataUbiquitousItemPercentDownloadedKey要么不存在,要么突然变为100一次该文件已下载。

相关代码:

 // get meta data item from query

 NSNumber *downloadProgress = [self.documentMetadataItem valueForAttribute:NSMetadataUbiquitousItemPercentDownloadedKey];
    NSLog(@"download status: %@", downloadProgress);

1 个答案:

答案 0 :(得分:1)

除了轮询NSMetadataItem进行更改之外,您应该只在每次收到更新时从NSMetadataQuery收听更新并获取NSMetadataUbiquitousItemPercentDownloadedKey一次(并更新用户界面)。< / p>

(我认为现有的NSMetadataItem对象在下载过程中不会更新,除了从下载过渡到完全下载之外)。

以下是使用KVO监听NSMetadataQuery更新的方法:

// 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;
    }
}

如果您仍然看到进度未更新,请向Apple提交错误。