而不是轮询NSMetadataItem
更改,您应该只侦听来自 的更新,并在每次收到更新时NSMetadataQuery
获取一次(并更新 UI)。NSMetadataUbiquitousItemPercentDownloadedKey
(我认为现有的 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 提交错误。