title: Swift NSURLSession: trouble when running multiple requests at the same time
body: <p>I'm currently building a weather application where I have one container <code>ViewController</code> managing multiple child <code>ViewControllers</code>, whose main views live inside the container <code>ViewController</code>'s <code>UIScrollView</code>.</p>
  
  <p>Each child VC has a location property, which is the class that holds all the weather information. This class has the following function for loading the data from the OpenWeatherMaps API:</p>
  
  <pre><code>func refresh(completionHandler: () -&gt; ()) {
      let session = NSURLSession.sharedSession()
      session.dataTaskWithURL(self._apiUrl) { (responseData: NSData?, response:NSURLResponse?, error: NSError?) -&gt; Void in
          if let data = responseData {
              do {
                  let json: AnyObject = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments)
                  // parse json
                  completionHandler()
              } catch let err as NSError {
                  print(err.debugDescription)
              }
          }
      }.resume()
  }
  </code></pre>
  
  <p>Then the child VC has a function calling the other function with the completionHandler running on the main thread (because I'm updating the user interface):</p>
  
  <pre><code>func refreshWeatherData() {
      location.refresh {
          dispatch_async(dispatch_get_main_queue(), {
              print("\(self.location.name)")
              self.forecastCollectionView.reloadData()
          })
      }
  }
  </code></pre>
  
  <p>And finally from the ContainerVC I'm calling the <code>refreshWeatherData</code> function on all my LocationVCs.</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      let zurich = WALocation(name: "Zürich", id: "7287650", country: "CH", longitude: 8.53071)
      let shanghai = WALocation(name: "Shanghai", id: "1796236", country: "CN", longitude: 121.45)
      let boston = WALocation(name: "Boston", id: "4183849", country: "US", longitude: -83.78)
      let vancouver = WALocation(name: "Vancouver", id: "6173331", country: "CA", longitude: -123.11)
  
      addLocationControllerForLocation(shanghai)
      locationControllers[0].refreshWeatherData()
      addLocationControllerForLocation(boston)
      locationControllers[1].refreshWeatherData()
      addLocationControllerForLocation(zurich)
      locationControllers[2].refreshWeatherData()
      addLocationControllerForLocation(vancouver)
      locationControllers[3].refreshWeatherData()
  }
  </code></pre>
  
  <p>Now the issue I encounter is that sometimes (not always), one two or three times, the JSONSerialisation throws an error (in the refresh function of the location):
  <code>Error Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo ``{NSDebugDescription=No value.}</code>
  When instead in my ContainerVC I do this:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      let zurich = WALocation(name: "Zürich", id: "7287650", country: "CH", longitude: 8.53071)
      let shanghai = WALocation(name: "Shanghai", id: "1796236", country: "CN", longitude: 121.45)
      let boston = WALocation(name: "Boston", id: "4183849", country: "US", longitude: -83.78)
      let vancouver = WALocation(name: "Vancouver", id: "6173331", country: "CA", longitude: -123.11)
  
      addLocationControllerForLocation(shanghai)
      locationControllers[0].refreshWeatherData()
      sleep(1)
      addLocationControllerForLocation(boston)
      locationControllers[1].refreshWeatherData()
      sleep(1)
      addLocationControllerForLocation(zurich)
      locationControllers[2].refreshWeatherData()
      sleep(1)
      addLocationControllerForLocation(vancouver)
      locationControllers[3].refreshWeatherData()
  }
  </code></pre>
  
  <p>NSJSONSerialisation never fails. This makes me think it only fails when multiple requests are running asynchronously.</p>
  
  <p>Any help would be greatly appreciated.</p>