views:

53

answers:

3

The Problem

  • I have an NSOperationQueue called logEntryGeneratorQueue
  • I want to wait until all operations on the queue have completed

If I use:

[logEntryGeneratorQueue waitUntilAllOperationsAreFinished];

it works fine if the thread adding to the queue is in the background itself.

However, if I'm running this code via a unit test, it'll be running on the main thread. So I came up with this "solution", which I really don't like:

if ([NSThread isMainThread]) {
    while ([[logEntryGeneratorQueue operations] count] > 0) {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
    }
} else {
    [logEntryGeneratorQueue waitUntilAllOperationsAreFinished];
}

This was always less than ideal, but has always worked fine on 10.5. However, now I've upgraded my project to using the 10.6 SDK, and this breaks.

On one test, it actually quit the test before it completed. I've no idea why - I assume it's something to do with the way NSOperationQueues work differently in 10.6 - they now use GCD.

What I've Tried

I've tried replacing the runUntilDate with sleep, which, as I thought, means every test pauses forever when it gets here.

My Question

Is there a better way to wait for an NSOperationQueue to finish on a main thread? If not, how can I get this code working under 10.6?

+1  A: 

I'd say that waitUntilAllOperationsAreFinished should work as expected on 10.6, no matter from what thread it's called. Since operation queues in 10.6 no longer use the run loop, there is no point in not blocking and having the loop run. Did you try to just call waitUntilAllOperationsAreFinished=

Max Seelemann
Yes, I did - the app just freezes. Even on 10.6. I really wish this worked irrespective of what thread you were on!
John Gallagher
Wierd. Have no idea then. May well be a bug or a hidden dependency...
Max Seelemann
+1  A: 

I agree with Max: -waitUntilAllOperationsAreFinished should work. Is your queue -suspended ?

Joshua Nozzi
Nope, turns out I was trying to run two different things on the main thread at the same time. Really obvious now that I look back at it...
John Gallagher
A: 

The Solution

I realised that my code was in an eternal loop because I was calling mergeChangesFromContextDidSaveNotification on the main thread whilst also waiting for the queue to finish on the main thread. And since the merge changes was called after waitUntilAllOperationsAreFinished, it never got executed.

I think the answer is to change where I run NSOperationQueues from. I shouldn't run an NSOperationQueue that deals with core data stuff on the main thread. And I shouldn't really be running this intensive stuff on the main thread for performance reasons anyway I guess.

John Gallagher