title: When does an NSOperationQueue start its first operation?
body: <p>I've created a test project in which I'm testing my assumptions about <code>NSOperation</code> and <code>NSOperationQueue</code> before using them in my main project.</p>
  
  <p>My code is pretty simple, so I'm going to include all of it here. This is using a command line Foundation project with ARC turned on.</p>
  
  <h2>Operation.h</h2>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface Operation : NSOperation
  
  @property (readwrite, strong) NSString *label;
  
  - (id)initWithLabel: (NSString *)label;
  
  @end
  </code></pre>
  
  <h2>Operation.m</h2>
  
  <pre><code>#import "Operation.h"
  
  @implementation Operation
  
  - (void)main
  {
      NSLog( @"Operation %@", _label);
  }
  
  - (id)initWithLabel: (NSString *)label
  {
      if (( self = [super init] )) {
          _label = label;
      }
      return self;
  }
  
  @end
  </code></pre>
  
  <h2>Main.m</h2>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  #import "Operation.h"
  
  int main(int argc, const char * argv[])
  {
  
      @autoreleasepool {
  
          NSOperationQueue *queue = [[NSOperationQueue alloc] init];
          queue.maxConcurrentOperationCount = 1;
  
          id create = [[Operation alloc] initWithLabel: @"create"];
          id update1 = [[Operation alloc] initWithLabel: @"update1"];
          id update2 = [[Operation alloc] initWithLabel: @"update2"];
  
          [update1 addDependency: create];
          [update2 addDependency: create];
  
          [queue addOperation: update1];
          [queue addOperation: create];
          [queue addOperation: update2];
  
          [queue waitUntilAllOperationsAreFinished];
  
      }
      return 0;
  }
  </code></pre>
  
  <p>My output looks like this:</p>
  
  <pre><code>2012-05-02 11:37:08.573 QueueTest[1574:1b03] Operation create
  2012-05-02 11:37:08.584 QueueTest[1574:1903] Operation update2
  2012-05-02 11:37:08.586 QueueTest[1574:1b03] Operation update1
  </code></pre>
  
  <p>Having written this and experimented with a few combinations, I found that when I reordered the queue setup like this:</p>
  
  <pre><code>    [queue addOperation: update1];
      [queue addOperation: create];
      [queue addOperation: update2];
  
      [update1 addDependency: create];
      [update2 addDependency: create];
  
      [queue waitUntilAllOperationsAreFinished];
  </code></pre>
  
  <p>I got the same output:</p>
  
  <pre><code>2012-05-02 11:38:23.965 QueueTest[1591:1b03] Operation create
  2012-05-02 11:38:23.975 QueueTest[1591:1b03] Operation update1
  2012-05-02 11:38:23.978 QueueTest[1591:1903] Operation update2
  </code></pre>
  
  <p>I should note that I have found in some runs that update2 is executed before update1, but <em>that</em> behaviour isn't surprising. Why should <code>NSOperationQueue</code> be deterministic when I haven't asked it to be?</p>
  
  <p>What I <em>do</em> find surprising is that somehow create is always executed before update1 and update2 even if everything is added to the queue before dependancies are added.</p>
  
  <p>Obviously, this is a silly thing to do, but it's led me to wonder: Is the delay between when I add an operation to the queue and when it's executed documented, or in any way predictable? Exactly when does <code>NSOperationQueue</code> start processing added operations?</p>
  
  <p>Really, most importantly, exactly what is <code>NSOperationQueue</code> waiting for and when will that wait bite me in some way I <strong>can't</strong> defend against?</p>