title: Managing a bunch of NSOperation with dependencies
body: <p>I'm working on an application that create contents and send it to an existing backend. Content is a title, a picture and location. Nothing fancy.</p>
  
  <p>The backend is a bit complicated so here is what I have to do :</p>
  
  <ul>
  <li>Let the user take a picture, enter a title and authorize the map to use its location</li>
  <li>Generate a unique identifier for the post</li>
  <li>Create the post on the backend</li>
  <li>Upload the picture</li>
  <li>Refresh the UI</li>
  </ul>
  
  <p>I've used a couple of NSOperation subclasses to make this work but I'm not proud of my code, here is a sample.</p>
  
  <pre><code>NSOperation *process = [NSBlockOperation blockOperationWithBlock:^{
      // Process image before upload
  }];
  
  NSOperation *filename = [[NSInvocationOperation alloc] initWithTarget: self selector: @selector(generateFilename) object: nil];
  
  NSOperation *generateEntry = [[NSInvocationOperation alloc] initWithTarget: self selector: @selector(createEntry) object: nil];
  
  NSOperation *uploadImage = [[NSInvocationOperation alloc] initWithTarget: self selector: @selector(uploadImageToCreatedEntry) object: nil];
  
  NSOperation *refresh = [NSBlockOperation blockOperationWithBlock:^{
      // Update UI
      [SVProgressHUD showSuccessWithStatus: NSLocalizedString(@"Success!", @"Success HUD message")];
  }];
  
  [refresh addDependency: uploadImage];
  
  [uploadImage addDependency: generateEntry];
  [generateEntry addDependency: filename];
  [generateEntry addDependency: process];
  
  [[NSOperationQueue mainQueue] addOperation: refresh];
  [_queue addOperations: @[uploadImage, generateEntry, filename, process] waitUntilFinished: NO];
  </code></pre>
  
  <p>Here are the things I don't like :</p>
  
  <ul>
  <li>in my createEntry: for example, I'm storing the generated filename in a property, which mees with the global scope of my class</li>
  <li>in the uploadImageToCreatedEntry: method, I'm using dispatch_async + dispatch_get_main_queue() to update the message in my HUD</li>
  <li>etc.</li>
  </ul>
  
  <p>How would you manage such workflow ? I'd like to avoid embedding multiple completion blocks and I feel like NSOperation really is the way to go but I also feel there is a better implementation somewhere.</p>
  
  <p>Thanks!</p>