When I run the following code, I can print a response almost immediately, however, it can take ten seconds or more to appear in my view. It seems like most similar problems are caused by the session and the handler being on different threads. But why does it work eventually? Very confused...
func downloadDetails(completed: DownloadComplete) {
let url = NSURL(string: _detailsURL)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url) { (data, response, error) -> Void in
do {
let dict = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! [String:AnyObject]
if let eyeColor = dict["eyeColor"] as? String {
self._eyeColor = eyeColor
}
}
catch {
print("json error: \(error)")
}
}
task.resume()
}