title: Swift dataTaskWithRequest completion block not executed
body: <p>I have a function which posts a dictionary back to the server and return the status code or error content when there's an error. It works fine sometimes, while the rest of the time the completion section is skipped.</p>
  
  <pre><code>func postData(url: String, query: NSDictionary) -&gt;NSObject? {
     var error: NSError?
     var result: NSObject? = nil
  
     let dest = NSURL("http://myUrl.com")
     let request = NSMutableURLRequest(URL: dest!)
     request.HTTPMethod = "POST"
     request.HTTPBody = NSJSONSerialization.dataWithJSONObject(query, options: NSJSONWritingOptions.allZeros, error: &amp;err)
     request.addValue("application/json", forHTTPHeaderField: "Content-Type")
     request.addValue("application/json", forHTTPHeaderField: "Accept")
  
     let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
                data, response, error in
                if(error != nil){
                     println(error)
                     result = error 
                     return
                }
                result = (response as! NSHTTPURLResponse).statusCode
                return
      }
      task.resume()
      return result
  }
  </code></pre>
  
  <p>I referred to <a href="https://stackoverflow.com/questions/27053877/nsurlsession-datataskwithrequest-not-being-called">NSURLSession dataTaskWithRequest not being called</a>, and knew that it may caused by executing time lag. However, since I need the status code (which returns nil so far) to determine the actions to do after the post, I'm wondering how I could solve this problem?</p>