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];
Then do this in your observeValueForKeyPath
:
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context
{
if (object == self.queue && [keyPath isEqual:@"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];
}
}
(This is assuming that your NSOperationQueue
is in a property named queue
)
EDIT: 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.