displaying alert after server response in swift
<p>I have this chunk of code which posts json object to server</p>

<pre><code>func postToServer(_ jsonObject: NSObject) -&gt; Void {

    var request = URLRequest(url: URL(string:url)!)

    let session = URLSession.shared
    request.httpMethod = "POST"

    //Note : Add the corresponding "Content-Type" and "Accept" header. In this example I had used the application/json.
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    request.httpBody = try! JSONSerialization.data(withJSONObject: jsonObject, options: [])

    let task = session.dataTask(with: request, completionHandler: { data, response, error in
        guard data != nil else {
            print("no data found: \(error)")
            return
        }

        do {
            if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
                print("Response: \(json)")

            } else {
                let jsonStr = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)// No error thrown, but not NSDictionary
                print("Error could not parse JSON: \(jsonStr)")

            }
        } catch let parseError {
            print(parseError)// Log the error thrown by `JSONObjectWithData`
            let jsonStr = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
            print("Error could not parse JSON 2: '\(jsonStr)'")

            self.alert(message: "test",title: "test")
        }
    })

    task.resume()
}
</code></pre>

<p>My problem is this: I want to display alert to the user after I get response from server. For that I'm using this simple function:    </p>

<pre><code> func alert(message: String, title: String = "") {
   let alertController = UIAlertController(title: title, message:   message, preferredStyle: .alert)
   let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
   alertController.addAction(OKAction)
   self.present(alertController, animated: true, completion: nil)}
</code></pre>

<p>But, my app blocks after this. Any ideas? </p>