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?
I can’t send notifications from my NSOperations, 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.
Use KVO to observe the
operationsproperty of your queue, then you can tell if your queue has completed by checking for[queue.operations count] == 0.Somewhere in the file you’re doing the KVO in, declare a context for KVO like this (more info):
When you setup your queue, do this:
Then do this in your
observeValueForKeyPath:(This is assuming that your
NSOperationQueueis in a property namedqueue)At some point before your object fully deallocs (or when it stops caring about the queue state), you’ll need to unregister from KVO like this:
Addendum: iOS 4.0 has an
NSOperationQueue.operationCountproperty, 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.