title: Why is NSURLSession dataTaskWithURL significantly slower vs NSData dataWithContentsOfURL?
body: <p>I'm trying to download a 94KB image file from my server. I've tried two ways to do this: using <code>NSURLSession dataTaskWithURL</code> and <code>NSData dataWithContentsOfURL</code>.</p>
  
  <p>NSURLSession:</p>
  
  <pre><code>Globals *global = [Globals getInstance];
  
  // GET request to /mobile/image
  NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/mobile/image/%@", [global rootUrl], self.photoId]];
  NSURLSession * session = [global session];
  NSDate *start = [NSDate date];
  [[session dataTaskWithURL:url completionHandler:^(NSData *imgData, NSURLResponse *response, NSError *error) {
      NSLog(@"Time taken: %f", [[NSDate date] timeIntervalSinceDate:start]);
      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
      if ([httpResponse statusCode] != 200) {
          // error
      } else {
          dispatch_async(dispatch_get_main_queue(), ^ {
              UIImage *image = [UIImage imageWithData:imgData];
  
              CGFloat width = self.photoImageView.frame.size.width;
              CGFloat height = image.size.height / image.size.width * width;
              self.imageHeight.constant = height;
  
              self.photoImageView.image = image;
          });
      }
  }] resume];
  </code></pre>
  
  <p>NSData:</p>
  
  <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
      Globals *global = [Globals getInstance];
  
      // GET request to /mobile/image
      NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/mobile/image/%@", [global rootUrl], self.photoId]];
      NSDate *start = [NSDate date];
      NSData *imgData = [NSData dataWithContentsOfURL:url];
      NSLog(@"Time taken: %f", [[NSDate date] timeIntervalSinceDate:start]);
      UIImage *image = [UIImage imageWithData:imgData];
  
      CGFloat width = self.photoImageView.frame.size.width;
      CGFloat height = image.size.height / image.size.width * width;
  
      dispatch_async(dispatch_get_main_queue(), ^{
          self.imageHeight.constant = height;
          self.photoImageView.image = image;
      });
  });
  </code></pre>
  
  <p>Here is the output for my NSLog for how long they took to complete:</p>
  
  <p>NSURLSession:</p>
  
  <pre><code>Time taken: 6.585221
  Time taken: 3.619189
  Time taken: 4.408179
  Time taken: 9.931350
  Time taken: 3.689192
  </code></pre>
  
  <p>NSData:</p>
  
  <pre><code>Time taken: 0.157747
  Time taken: 0.135785
  Time taken: 0.576947
  Time taken: 0.462661
  Time taken: 0.337266
  </code></pre>
  
  <p>Taking 3~10 seconds to download a simple &lt;100KB image file is unacceptable, so I'm currently just sticking with <code>NSData</code>, but I need to use <code>NSURLSession</code> later for another similar image download task because I need login sessions. </p>
  
  <p>I'm wondering if I've accidentally discovered a weird bug with <code>NSURLSession</code>, if it's just supposed to be much slower because it has all the overhead of sessions, or if I'm doing something wrong.</p>
  
  <p>Edit:</p>
  
  <p>I figured it out, thanks for all your help! </p>
  
  <p>I created a whole new project with all the connections of this one, and it performed fine. </p>
  
  <p>Problem: The previous VC that before this was calling dataTask multiple times. I was completely baffled why that would affect this VC. Turned out that the class was called ViewController (default name), and this VC was inheriting ViewController instead of UIViewController. </p>
  
  <p>Therefore, calling [super viewDidLoad] from the viewDidLoad of this ViewController was actually calling the previous VC and running all those dataTasks all over again.</p>
  
  <p>When in doubt, check all the other connections in the session.</p>