NSURLSession 后台下载 - 通过网络故障恢复 [英] NSURLSession background download - resume over network failure

查看:36
本文介绍了NSURLSession 后台下载 - 通过网络故障恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

After reading the Apple documentation about the background download with the new iOS7 api (NSURLSession), I'm a bit disappointed. I was sure that Apple was managing the pause/resume over the network availability in the background (or provide an option to do so) but no…

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

So reading the documentation, this is what we've got:

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

当任何任务完成时,NSURLSession 对象调用委托的 URLSession:task:didCompleteWithError: 方法与错误对象,如果任务成功完成,则为零.如果任务是可恢复下载任务,NSError 对象的 userInfo 字典包含 NSURLSessionDownloadTaskResumeData 键的值.你的应用程序应使用可达性 API 来确定何时重试,以及然后应该调用 downloadTaskWithResumeData: 或downloadTaskWithResumeData:completionHandler: 创建一个新的下载任务继续下载.转到第 3 步(创建和恢复任务对象).

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).

目前我明白了解决方案,但我的问题是:什么架构最适合处理网络丢失并在后台恢复下载?

So far I understand the solution, but my question is: What architecture is the best to handle the loss of the network and resume downloading in the background?

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

On my side I'm using reachability and each time the network is available, I resume all tasks (referenced over a NSArray when creating), and suspends them when network is lost. This works well in foreground but for the background I need help on the following points:

  • 如果我的应用在前台没有连接,如果我在没有连接的情况下进入后台,我的所有任务都会暂停,如果网络可用就不会回来......

  • If my app has no connectivity in foreground, if I go to the background without connectivity all my tasks remains suspended and won't came back if network is available…

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

Losing network in background, stop all my downloads/tasks. Scenario:

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

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

How do I address these points? Have I missed something?

推荐答案

由于我还在后台,我无法在不启动应用程序的情况下恢复网络时触发恢复"...

As I'm still in background I cannot trigger a "resume" when network is back without launching the application…

可以使用background fetch",当应用通过fetch启动时,可以检查网络并恢复下载任务.

you can use "background fetch",when the app is launched by fetch,then you can check network and resume the download task.

这篇关于NSURLSession 后台下载 - 通过网络故障恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆