title: NSURLSessionDataTask causes high CPU usage
body: <p>I'm using a <code>NSURLSessionDataTask</code> to feed data into an audio stream in the background (not really relevant in this case).</p>
  
  <p>The problem I'm encountering happens with just the downloading code - I've isolated this and simply discard the data, so no other parts of the system are affecting it.</p>
  
  <p>I've observed that if I create the <code>NSURLSession</code> with a config (<code>NSURLSessionConfiguration</code>) created using <code>backgroundSessionConfigurationWithIdentifier</code> then the CPU usage while downloading is low (&lt;5%).  Although this works (95% of the time), I believe it's not a supported configuration.  I also only need this to work when the app is in the foreground.</p>
  
  <p>So, instead, I create the <code>NSURLSession</code> with a config created using <code>ephemeralSessionConfiguration</code>, this also stops it using the disk as a background cache and should therefore use the least CPU.  However, in this scenario CPU usage rockets to between 70% &amp; 80%.</p>
  
  <p>The same high CPU usage also occurs with config created using <code>defaultSessionConfiguration</code> or even getting the shared <code>NSURLSession</code> using <code>[NSURLSession sharedSession]</code>.</p>
  
  <p>The data throughput is unchanged between each scenario - just not running in the background causes high CPU usage.</p>
  
  <p>I'm running on iOS 9.1 using SDK 9.1.  It happens in both the simulator and on-device.</p>
  
  <p><strong>Update 16th Nov</strong> - As George pointed out, it appears then the didReceiveData method is thrashed.  The only way I've found to deal with this is to add in a call to </p>
  
  <pre><code>[NSThread sleepForTimeInterval:0.25];
  </code></pre>
  
  <p>which seems pretty drastic, but takes the CPU from around 60-80% when downloading, to around 10-12%.  The download is running in it's own thread, so it's only the download that is slowed down.  In fact, it's not really slowed down - the <code>didReceiveData</code> just receives data in much larger chunks.  In my app this doesn't matter.</p>