I'm not sure why my app crashes due to a "request timed out" error. It absolutely doesn't make sense to me because I'm getting a response back with the data i'm expecting.
Some context:
Every time the user scrolls the mapview, I make a call to the server to retrieve some info about a location, which comes back fine, I believe. Do I need to close the connection after I'm done? Is it because i'm scrolling too much and in turn i'm making so many requests that it overwhelms the server? I absolutely have no clue as to why this might be happening, but I do know that the data I'm looking to get back is coming back successfully, and then out of no where it'll say "request timed out" and I got a nil for the response
. Below is my code. Thank you!
Additionally, how would I handle a timed out request so my app does not crash?
let task = session.dataTaskWithRequest(request) { (data, response, error) -> Void in
let httpResponse = response as? NSHTTPURLResponse
if httpResponse?.statusCode != 200 {
print(httpResponse?.statusCode)
}
if error != nil {
print("Localized description error: \(error!.localizedDescription)")
}
do {
//Store JSON data into dictionary
self.locationsObjectDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSMutableDictionary
//print(self.locationsObjectDictionary)
} catch {
print("JSON object could not be retrieved: \(error)")
}
completion()
}
// Start the session
task.resume()
}
let qos = DISPATCH_QUEUE_PRIORITY_HIGH
let queue = dispatch_get_global_queue(qos, 0)
dispatch_async(queue) {
self.fetchLocations() {
self.addLocationInfoToPins()
dispatch_async(dispatch_get_main_queue(), {
self.mapView.addAnnotations(self.locationPins)
})
}
}