我正在研究從我們的服務器下載文件的應用程序(相當大的文件)。我在前臺和後臺管理下載文件(無需關閉應用程序)。我的問題是,用戶使用多任務屏幕故意關閉應用程序。使用NSURLSessionDownloadTask時使用resumeData的正確方法是什麼?
在這種情況下,我在didCompleteWithError方法得到一個回調和接收具有error.userInfo包含恢復數據的NSURLSessionDownloadTaskResumeData項下NSData對象錯誤。
問題是:這種情況應該如何處理?我是否應該立即使用didCompleteWithError方法中的downloadTaskWithResumeData來啓動另一個任務,或者我需要將resumeData NSData對象保存在UserDefaults中,並在下一次應用程序運行時使用它?
我試圖做這樣的事情在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)
}
}
}
但有些東西不工作的權利,而我真的不能調試它,因爲我需要殺掉基本上殺死應用程序調試過程。
在此先感謝您的幫助。