gpt4 book ai didi

ios - 使用 NSURLSessionDownloadTask 时使用 resumeData 的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-28 08:56:07 24 4
gpt4 key购买 nike

我正在开发一个从我们的服务器下载文件(相当大的文件)的应用程序。我在前台和后台管理下载文件(不关闭应用程序)。我的问题是用户使用多任务屏幕故意关闭应用程序

在这种情况下,我在 didCompleteWithError 方法上获得回调并接收到具有 error.userInfo 的错误,其中包含 下的恢复数据 NSData 对象>NSURLSessionDownloadTaskResumeData 键。

问题是:应该如何处理这种情况?我应该立即使用 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)
}
}
}

但是有些东西工作不正常,我无法真正调试它,因为我需要终止应用程序,这基本上会终止调试的进程。

最佳答案

它不工作,因为应用程序已经被终止,只给你很短的时间(大约 5 秒)来保存你的东西。

要处理您的案例,请参阅 apple docs 中的以下注释

If an iOS app is terminated by the system and relaunched, the app can use the same identifier to create a new configuration object and session and retrieve the status of transfers that were in progress at the time of termination. This behavior applies only for normal termination of the app by the system. If the user terminates the app from the multitasking screen, the system cancels all of the session’s background transfers.

所以看起来你必须在你的情况下定义一些自定义的东西来处理你所说的 NUUser 默认值,但我建议使用 NSURLCache。

关于ios - 使用 NSURLSessionDownloadTask 时使用 resumeData 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32994644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com