gpt4 book ai didi

ios - NSURLSession 后台下载 - 通过网络故障恢复

转载 作者:IT王子 更新时间:2023-10-29 08:09:34 24 4
gpt4 key购买 nike

阅读有关使用新的 iOS7 api (NSURLSession) 进行后台下载的 Apple 文档后,我有点失望。我确信 Apple 正在后台通过网络可用性管理暂停/恢复(或提供这样做的选项)但没有......

所以阅读文档,这就是我们得到的:

https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/NSURLSessionConcepts/NSURLSessionConcepts.html

When any task completes, the NSURLSession object calls the delegate’s URLSession:task:didCompleteWithError: method with either an error object, or nil if the task completed successfully. If the task is a resumable download task, the NSError object’s userInfo dictionary contains a value for the NSURLSessionDownloadTaskResumeData key. Your app should use reachability APIs to determine when to retry, and should then call downloadTaskWithResumeData: or downloadTaskWithResumeData:completionHandler: to create a new download task to continue that download. Go to step 3 (creating and resuming task objects).

到目前为止,我了解了解决方案,但我的问题是:哪种架构最适合处理网络丢失并在后台恢复下载?

在我这边,我使用可达性,每次网络可用时,我都会恢复所有任务(创建时通过 NSArray 引用),并在网络丢失时暂停它们。这在前台工作得很好,但对于后台我需要以下几点的帮助:

  • 如果我的应用在前台没有连接,如果我在没有连接的情况下进入后台,我的所有任务都将保持暂停状态,并且在网络可用时不会返回……

  • 在后台失去网络,停止我所有的下载/任务。场景:

    • 在前台,我开始下载我的任务
    • 我进入后台并在 10 秒后切换到“aireplan 模式”
    • 我所有的任务都出错了。所以在方法 URLSession:task:didCompleteWithError: 我恢复他们使用downloadTaskWithResumeData 或者如果我不能(因为有些没有足够的恢复数据)我正在创建一个没有恢复它的新任务(除非当时网络恢复正常)。
    • 然后我把wifi打开了
    • 因为我仍然在后台,所以当网络恢复时我无法在不启动应用程序的情况下触发“恢复”……

我该如何解决这些问题?我错过了什么吗?

最佳答案

因为我仍然在后台,所以当网络恢复时我无法在不启动应用程序的情况下触发“恢复”......


您可以使用“后台获取”,当应用程序被获取启动时,您可以检查网络并恢复下载任务。

关于ios - NSURLSession 后台下载 - 通过网络故障恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19184490/

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