title: How should I use NSURLSession in backgroundConfiguration to upload chunks of multiple files to Amazon S3 ( iOS7 )
body: <p>Currently I am working on a project where we need to send a set of photo's and video's to a S3 amazon server. The flow is like this:</p>
  
  <p>-first we ask api to start a transfer and we get an id back (api call)</p>
  
  <p>-transfer id -> request file upload at api -> file_id as response (api call)</p>
  
  <p>-file id -> request chunk upload at api -> amazon data as response (api call)</p>
  
  <p>-upload chunck -> in the NSURLSession in configured in 
  backgroundConfiguration (5mb each upload)</p>
  
  <p>-finish file upload after last chunck-upload(api call)</p>
  
  <p>-finish transfer after last file-upload(api call)</p>
  
  <hr>
  
  <p>We need to use the api and make calls to it.. also when the app is running in the background. </p>
  
  <p>So what I thought was to use the AFNetworking 2.0 that can upload files in the background and then runs a completion block. </p>
  
  <hr>
  
  <p>In that completion block the code is like:</p>
  
  <pre><code> _managerBackground = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  
  
      - (void)uploadChunkInTheBackGround:(RequestChunkUploadResponse *)requestChunkUploadResponse
      {
          UploadChunkRequest *chunkUploadRequest = [[UploadChunkRequest alloc]initWithRequestChunkUploadResponse:requestChunkUploadResponse];
  
          APIService *weakSelf = (APIService *)self;
  
           NSURLSessionUploadTask *uploadTask = [_managerBackground uploadTaskWithRequest:chunkUploadRequest.request fromFile:chunkUploadRequest.fileLocationURL progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
         //code to prepare recursive upload and files
          [weakSelf uploadChunkInTheBackGround:chunkUploadResponse];
      }];
  </code></pre>
  
  <p>So the difficulty is that we like to execute code and also try do a upload task in the background of iOS7</p>
  
  <p>When the app is connected to the debugger (Xcode) the above sample works. But without it provides this error in the console:</p>
  
  <pre><code>&lt;BKProcessAssertion: 0x178468380&gt; identifier: com.apple.nsnetworkd.handlesession process: YourApp[675] permittedBackgroundDuration: 30.000000 reason: backgroundDownload owner pid:111 preventSuspend  preventThrottleDownUI  preventIdleSleep  preventSuspendOnSleep
  </code></pre>
  
  <hr>
  
  <p>So thats not cool :(</p>
  
  <p>Is there a way to run code and also uploading the files in the background..?? Do you have any experience with this?? Or do we ask to much of iOS7??</p>
  
  <p>I hope you can share your thoughts.</p>
  
  <p>Thanks,</p>
  
  <p>Kind Regards,</p>
  
  <p>Bart Schoon</p>