title: How should I use GCD dispatch_barrier_async in iOS (seems to execute before and not after other blocks)
body: <p>I'm trying to synchronize the following code in iOS5:</p>
  
  <ol>
  <li>an object has a method which makes an HTTP request from which it
  gets some data, including an URL to an image</li>
  <li>once the data arrives, the textual data is used to populate a
  CoreData model</li>
  <li>at the same time, a second thread is dispatched async to download
  the image; this thread will signal via KVO to a viewController when
  the image is already cached and available in the CoreData model.</li>
  <li>since the image download will take a while, we immediately return
  the CoreData object which has all attributes but for the image to
  the caller.</li>
  <li>Also, when the second thread is done downloading, the CoreData model
  can be saved.</li>
  </ol>
  
  <p>This is the (simplified) code:</p>
  
  <pre><code>- (void)insideSomeMethod
  {
      [SomeHTTPRequest withCompletionHandler:
       ^(id retrievedData) 
       {
           if(!retrievedData)
           {
               handler(nil);
           }
  
           // Populate CoreData model with retrieved Data...
  
           dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
               NSURL* userImageURL = [NSURL URLWithString:[retrievedData valueForKey:@"imageURL"]];
               aCoreDataNSManagedObject.profileImage = [NSData dataWithContentsOfURL:userImageURL];
           });
  
           handler(aCoreDataNSManagedObject);
           [self shouldCommitChangesToModel];
       }];
  }
  
  - (void)shouldCommitChangesToModel
  {
      dispatch_barrier_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
          NSError *error = nil;
          if(![managedObjectContext save:&amp;error]) 
          {
              //  Handle error
          }  
      });
  }
  </code></pre>
  
  <p>But what's going on is that the barrier-based save-block is always executed before the the image-loading block. That is,</p>
  
  <pre><code>dispatch_barrier_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
              NSError *error = nil;
              if(![managedObjectContext save:&amp;error]) 
              {
                  //  Handle error
              }  
          });
  </code></pre>
  
  <p>Executes before:</p>
  
  <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
                   NSURL* userImageURL = [NSURL URLWithString:[retrievedData valueForKey:@"imageURL"]];
                   aCoreDataNSManagedObject.profileImage = [NSData dataWithContentsOfURL:userImageURL];
               });
  </code></pre>
  
  <p>So obviously I'm not really dispatching the image-loading block before the barrier, or the barrier would wait until the image-loading block is done before executing (which was my intention).</p>
  
  <p>What am I doing wrong? how do I make sure the image-loading block is enqueued before the barrier block?</p>