Problem
I recently updated my iPhone to iOS 10.3.1 and Xcode to Version 8.3.2. Currently, I have an app which runs a few URLRequest
s once my app launches.
However, after updating both iOS and Xcode, the app occasionally return an error message:
error in connection_block_invoke_2: Connection interrupted
The message is quite vague but I assumed it has something to do with the URLRequest
s since it mentions "Connection interrupted".
Whenever this error message appears, it will "freeze" the app for ~5s before getting the data from the server.
Question
So, what does this error message actually mean? And how can I minimise it or fix it?
Example of one URLRequest
:
func checkLogin () {
let username = txtUsername.text!
let password = txtPassword.text!
let post = "username=\(username)&password=\(password)"
NSLog("PostData: %@",post);
let url:URL = URL(string:"https://example.com/login.php")!
let postData = post.data(using: .utf8)!
let postLength = String( postData.count )
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if error != nil {
return
}
if let jsonData = (try? JSONSerialization.jsonObject(with: data!, options: [])) as? [String:Any] {
let success = jsonData["success"] as! Int
if success == 1 {
//do something,
}
else {
//show alert
}
}
})
task.resume()
}
Duplicates
The following questions does not solve my problem as:
What is "error in __connection_block_invoke_2: Connection interrupted" in iOS?
- I didn't receive any memory warning so the answer does not apply.
iOS: error in __connection_block_invoke_2: Connection interrupted
- I didn't use
AVCaptureMovieFileOutput
andAVCaptureVideoDataOutput
at all in my app, thus the answer also doesn't apply.
- I didn't use
error in __connection_block_invoke_2: Connection interrupted on iPad & Xcode 7 error message "error in __connection_block_invoke_2: Connection interrupted" when app runs on iPad
- No answer at all.