title: IOS, Swift, No way to access main thread in NSURLSession
body: <p>I am creating an iOS app using swift. I would like to achieve some GET or POST HTTP request. I know that Alamofire exists but i want to create my own functions. </p>
  
  <p>What I have done : </p>
  
  <pre><code>import Foundation
  
  class DatabaseRequest:NSObject{
  
      class func GET(urlAsString:String, completion : (response:NSURLResponse, result:AnyObject?, error:String?)-&gt; Void){
  
          let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
          let session = NSURLSession(configuration: configuration)
  
          let url = NSURL(string: urlAsString)
          let urlRequest = NSMutableURLRequest(URL: url!)
          urlRequest.HTTPMethod = "GET"
  
          session.dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) -&gt; Void in
              if let error = error{
                  dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                      completion(response: response, result: nil, error: "GET Connection error : \(error.localizedDescription)")
                  })
              }else{
                  var error:NSError?
  
                  let JSON_Object:AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &amp;error)
                  if let error = error{
                      dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                          completion(response: response, result: nil, error: "GET JSONSerialization error: \(error.localizedDescription)")
                      })
                  }else{
                      if let result:AnyObject = JSON_Object{
                          //The third time I use this class function, no way to access the main thred to send data
                          dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                              completion(response: response, result: JSON_Object, error: nil)
                          })
                      }else{
                          dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                              completion(response: response, result: nil, error: nil)
                          })
                      }
                  }
              }
  
              session.finishTasksAndInvalidate()
  
          }).resume()
  
      }
  
      class func POST(urlAsString:String, parameters:[String:AnyObject], completion:(response:NSURLResponse?, result:AnyObject?, error:String?)-&gt;Void){
          println("POST used")
          let configuration = NSURLSessionConfiguration.ephemeralSessionConfiguration()
          let session = NSURLSession(configuration: configuration)
  
          var errorHTTPBody:NSError?
          let url = NSURL(string: urlAsString)
          let urlRequest = NSMutableURLRequest(URL: url!)
          urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
          urlRequest.addValue("application/json", forHTTPHeaderField: "Accept")
          urlRequest.HTTPMethod = "POST"
          urlRequest.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &amp;errorHTTPBody)
  
          if let error = errorHTTPBody{
              dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                  completion(response: nil, result: nil, error: "POST errorHTTPBody: \(error.localizedDescription)")
              })
              return
          }
  
          session.dataTaskWithRequest(urlRequest, completionHandler: { (data, response, error) -&gt; Void in
              println(data)
              if let error = error{
                  dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                      completion(response: response, result: nil, error: "POST Connection error : \(error.localizedDescription)")
                  })
              }else{
                  var error:NSError?
                  var JSON_Object:AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &amp;error)
                  if let error = error{
                      dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                          completion(response: response, result: nil, error: "POST JSONSerialization error : \(error.localizedDescription)")
                      })
                  }else{
                      if let result:AnyObject = JSON_Object{
                          dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                              completion(response: response, result: JSON_Object, error: nil)
                          })
                      }else{
                          dispatch_async(dispatch_get_main_queue(), { () -&gt; Void in
                              completion(response: response, result: nil, error: nil)
                          })
                      }
                  }
              }
  
              session.finishTasksAndInvalidate()
  
          }).resume()
  
      }
  
  }
  </code></pre>
  
  <p>The interesting part is mostly the GET function (since the POST function works the same). Well, everything seems to work fine. I achieve to use this GET function 2 times but the third time I want to use it, there is no way to access the main thread to send data. I can log something just before my commentary <code>//The third time I use this class function, no way to access the main thread to send data</code> but nothing logs in the <code>dispatch_async(dispatch_get_main_queue(),block)</code>.</p>
  
  <p>Any idea?</p>