What I Want
I've got an NSOperationQueue in my application and it's critical that all operations get processed before the application quits. I've got the following code on quit to make sure the NSOperationQueue is empty before quitting:
if ([NSThread isMainThread]) {
while ([[operationQueue operations] count] > 0) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];
}
} else {
[operationQueue waitUntilAllOperationsAreFinished];
}
I've encountered a problem with my code before where an NSOperation "froze" before finishing and it blocked the whole queue. Obviously, in situations like this, my code should throw an exception, which it now does.
But I still want to guard against anything weird happening where operations stop and block the queue, resulting in the application never quitting and the fault not being caught.
My Rubbish Solution
I'm considering putting a timeout on the queue finishing its operations. Then when it does timeout, I could throw an exception then the problem could be caught.
However, this doesn't feel right. Ideally, I don't want to be reliant upon timers to make sure work is done.
My Question
Is there a better, fail safe, way of making sure my operations haven't frozen?