I'm working on an application that download files from our server (fairly large files). I manage the download files in foreground and background (without closing the application). My problem is with the case that the user intentionally closes the application using the multi-tasking screen.
In this case I'm getting a callback on didCompleteWithError
the method and receive the error that has error.userInfo
that contains the resume data NSData object under the NSURLSessionDownloadTaskResumeData
key.
The question is: How this situation should be handled? Should I immediately start another task using the downloadTaskWithResumeData from the didCompleteWithError
method, or I need to save the resumeData NSData object in the UserDefaults
and use it the next time the application runs?
I tried to do something like this in the didCompleteWithError
:
if (error?.userInfo[NSURLSessionDownloadTaskResumeData] != nil) {
Logger.printLogToConsole(self.TAG, aMethodName: __FUNCTION__, aMessage: "Resume data was found");
let req = task.originalRequest
let languageCodeWrapped: AnyObject? = NSURLProtocol.propertyForKey("languageCode", inRequest:req!)!
if let languageCode = languageCodeWrapped {
SessionDownloader.sharedInstance.downLoadWithResumeData(languageCode as! String, aResumeData: error?.userInfo[NSURLSessionDownloadTaskResumeData] as! NSData)
if taskDelegate != nil {
DownloadSessionDelegate.sharedInstance.setDelegateForTaskId(taskDelegate, taskId: "\(task.taskIdentifier)", code: languageCode as! String)
}
}
}
But something there is not working right, and I can't really debug it because I need to kill the app which basically kills the debugged process.