NSURLSession后台下载 - 恢复网络故障 [英] NSURLSession background download - resume over network failure
问题描述
在阅读了有关使用新iOS7 api(NSURLSession)进行后台下载的Apple文档后,我有点失望。我确信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:
当任何任务完成时,NSURLSession对象使用
错误对象调用
委托的URLSession:task:didCompleteWithError:方法,如果任务成功完成,则调用nil。
如果任务是可恢复的下载任务,则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:
- 在前台,我开始下载我的任务
- 我去了背景和10s后切换到aireplan模式
- 我的所有任务都出错了。所以在方法URLSession:task:didCompleteWithError:我使用
downloadTaskWithResumeData恢复它们,或者如果我不能(因为有些没有
足够的恢复数据)我正在创建一个新任务而不恢复它(当网络回来的时候除外)。 - 然后我把wifi上了
- 因为我还在后台,所以我无法触发网络回来时恢复而不启动应用程序......
- In foreground, I start downloading my tasks
- I go to background and after 10s switch to "aireplan mode"
- All my tasks got an error. So in the method URLSession:task:didCompleteWithError: I resume them using downloadTaskWithResumeData or if I can't (because some have not enough resume data) I'm creating a new task without resume-ing it (except if network is back at that time).
- Then I put the wifi up
- As I'm still in background I cannot trigger a "resume" when network is back without launching the application…
我如何解决这些问题?我错过了什么吗?
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…
你可以使用后台获取,当通过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屋!