问题描述
NSOperationQueue
的 waitUntilAllOperationsAreFinished
,但我不希望同步等待。我只是想隐藏进度指示器在UI队列时完成。
NSOperationQueue
has waitUntilAllOperationsAreFinished
, but I don't want to wait synchronously for it. I just want to hide progress indicator in UI when queue finishes.
什么是实现这一目标的最佳方式?
What's the best way to accomplish this?
我无法从我的的NSOperation
S发送通知,因为我不知道哪一个会是最后一次,和 [队列操作]
可能不是空的(或更糟 - 重新填充)。在收到通知时
I can't send notifications from my NSOperation
s, because I don't know which one is going to be last, and [queue operations]
might not be empty yet (or worse - repopulated) when notification is received.
推荐答案
使用志愿观察操作
属性队列中,那么你可以告诉我们,如果您的队列已完成通过检查 [queue.operations计数] == 0
。
Use KVO to observe the operations
property of your queue, then you can tell if your queue has completed by checking for [queue.operations count] == 0
.
当您设置你的队列中,做到这一点:
When you setup your queue, do this:
[self.queue addObserver:self forKeyPath:@"operations" options:0 context:NULL];
然后做这在你的 observeValueForKeyPath
:
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if (object == self.queue && [keyPath isEqualToString:@"operations"]) {
if ([self.queue.operations count] == 0) {
// Do something here when your queue has completed
NSLog(@"queue has completed");
}
}
else {
[super observeValueForKeyPath:keyPath ofObject:object
change:change context:context];
}
}
(这是假设你的 NSOperationQueue
是一个命名属性队列
)
结果
编辑:的iOS 4.0现在有一个 NSOperationQueue.operationCount
属性,根据文档这是国际志愿者组织兼容。这个答案仍然会在iOS的4.0然而工作,所以它仍然是有用的向后兼容。
iOS 4.0 now has an NSOperationQueue.operationCount
property, which according to the docs is KVO compliant. This answer will still work in iOS 4.0 however, so it's still useful for backwards compatibility.
这篇关于得到通知时NSOperationQueue完成所有任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!