title: AFNetworking and background transfers
body: <p>I'm a bit confuse of how to take advantage of the new iOS 7 <code>NSURLSession</code> background transfers features and <a href="https://github.com/AFNetworking/AFNetworking/" rel="noreferrer">AFNetworking</a> (versions 2 and 3).</p>
  
  <p>I saw the <code>WWDC 705 - What’s New in Foundation Networking</code> session, and they demonstrated background download that continues after the app terminated or even crashes.  </p>
  
  <p>This is done using the new API <code>application:handleEventsForBackgroundURLSession:completionHandler:</code> and the fact that the session's delegate will eventually get the callbacks and can complete its task.</p>
  
  <p>So I'm wondering how to use it with AFNetworking (if possible) to continue downloading in background.  </p>
  
  <p>The problem is, AFNetworking conveniently uses block based API to do all the requests, but if the app terminated or crashes those block are also gone.  So how can I complete the task?  </p>
  
  <p>Or maybe I'm missing something here...  </p>
  
  <p>Let me explain what I mean:</p>
  
  <p>For example my app is a photo messaging app, lets say that I have a <code>PhotoMessage</code> object that represent one message and this object has properties like  </p>
  
  <ul>
  <li><code>state</code> - describe the state of the photo download.  </li>
  <li><code>resourcePath</code> - the path to the final downloaded photo file.</li>
  </ul>
  
  <p>So when I get a new message from the server, I create a new <code>PhotoMessage</code> object, and start downloading its photo resource.</p>
  
  <pre><code>PhotoMessage *newPhotoMsg = [[PhotoMessage alloc] initWithInfoFromServer:info];
  newPhotoMsg.state = kStateDownloading;
  
  self.photoDownloadTask = [[BGSessionManager sharedManager] downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
      NSURL *filePath = // some file url
      return filePath;
  } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
      if (!error) {
          // update the PhotoMessage Object
          newPhotoMsg.state = kStateDownloadFinished;
          newPhotoMsg.resourcePath = filePath;
      }
  }];
  
  [self.photoDownloadTask resume];   
  </code></pre>
  
  <p>As you can see, I use the completion block to update that <code>PhotoMessage</code> object according to the response I get.    </p>
  
  <p>How can I accomplish that with a background transfer? This completion block won't be called and as a result, I can't update the <code>newPhotoMsg</code>.</p>