I have an application that retrieves JSON data from my server and enters it in to my Core Data model. I have this working reasonably well, minus a few issues with random EXC_BAD_ACCESS
errors that I haven't figured out yet. During this dataTask, it can retrieve large amounts of data so it can take a while to complete.
I'm looking for a way to let the user suspend the app the dataTask will continue to retrieve the data. I seen NSURLSession has a background mode, but seen it only supports upload and download tasks.
Is there a way to support this?
My dataTask function:
class func Request(file: String, withData: String?, completion: (NSData -> Void)) {
let url = NSURL(string: "\(root)\(file)")!
let request = NSMutableURLRequest(URL: url)
if let sentData = withData {
request.HTTPMethod = "POST"
request.HTTPBody = sentData.dataUsingEncoding(NSUTF8StringEncoding)
}
let dataTask = session.dataTaskWithRequest(request) {
data, response, error in
if error != nil {
if error?.domain == NSURLErrorDomain && error?.code == NSURLErrorTimedOut {
print("Data task timed out")
}
} else {
let httpResponse : NSHTTPURLResponse = response as! NSHTTPURLResponse
if httpResponse.statusCode == 200 {
completion(data!)
} else {
print("Request failed with status code: \(httpResponse.statusCode)")
}
}
}
dataTask.resume()
}