title: Prevent NSURLSession from caching responses
body: <p>Why does it cache responses. It returns previously fetched responses. It even works if turning off the network connection. Resetting the iOS simulator did not seem to work either. Making a request, then again with internet offline does work (cached).</p>
  
  <pre><code>public let urlSession: NSURLSession
  
  public init()
  {
      // ...
  
      var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
      configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
      urlSession = NSURLSession(configuration: configuration)
  }
  
  func makeRequest(path: String, httpMethod:String, body:String?, baseUrl: String?, headers:[String:String]=[:], callback: JsonRequestCallback)
  {
      let urlString = (baseUrl ?? customOAuth2Manager.API_URL) + path
      let url = NSURL(string: urlString)
      let request = oauthInstance.request(forURL: url!)
  
      request.HTTPMethod = httpMethod
      self.customOAuth2Manager.setupRequest(request)
  
      for (key, value) in headers {
          request.setValue(value, forHTTPHeaderField:key)
      }
  
      if let body = body where body != "" {
          let postData = (body as NSString).dataUsingEncoding(NSUTF8StringEncoding)
          request.HTTPBody = postData
      }
  
      let task = urlSession.dataTaskWithRequest(request) { data, response, error in
          self.parseData(data, response:response, error:error, body:body, callback: callback)
      }
      task.resume()
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I managed to solve it by calling this code from <code>didFinishLaunching</code> in the <code>AppDelegate</code>:</p>
  
  <pre><code>func removeUrlCache()
  {
      NSURLCache.setSharedURLCache(NSURLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil))
  }
  </code></pre>
  
  <p>However, I'm still curious why my original code does not work. It also feels a bit ugly to disable cache for all the app to solve my problem.</p>