NSURLSession从崩溃恢复 [英] NSURLSession resume from crash

查看:144
本文介绍了NSURLSession从崩溃恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有任何关于如何重新连接到NSURLSession在崩溃后下载的提示。



如果我的应用程序崩溃,我相信下载在设备上继续,然后当我的应用程序重新启动时,我可以使用相同的sessionID:

  NSURLSessionConfiguration.backgroundSessionConfiguration(sessionID)

然而,我应该调用session.getTasksWithCompletionHandler来查看是否有任务?文件不清楚。



我的用户界面有一个下载进度条,用于每个文件下载,所以理想情况下,当我重新启动应用程序时,它会尝试重新连接并挂接进度回溯到UI。



目前正在发生的事情是我重新启动下载,而我的进度条闪烁,因为有2个下载正在进行 - 旧的和新的,然后它会进入一个状态...

解决方案

确定这里是我发现的,在 URL系统编程指南


在iOS和OS X中,当用户重新启动应用程序时,您的应用程序
应立即创建后台配置对象与
相同的标识符,当您的
应用程序最后一次运行时,任何具有未完成任务的会话,然后为每个
配置对象创建一个会话。这些新会话类似地自动
重新与正在进行的背景活动相关。


所以一旦我再次创建会话并连接代理,代理方法再次开始触发,我使用getTasksWithCompletionHandler查看是否有任何任务正在进行中,并将其返回到我的UI和系统的其余部分


Has anyone got any tips on how to reconnect to a download in a NSURLSession after a crash.

If my app crashes, I believe the download continues on the device anyway, and then when my app relaunches I can use the same sessionID in:

NSURLSessionConfiguration.backgroundSessionConfiguration(sessionID)

However, am I then supposed to call 'session.getTasksWithCompletionHandler' to see if there are any tasks? The docs aren't clear.

My UI has a download progress bar for each file download, so ideally when I relaunch the app, it would try and reconnect and hook the progress back up to the UI.

At the moment what is happening is I restart the download, and my progress bar flickers because there are 2 downloads in progress - the old one and a new one, and then it gets itself into a state...

解决方案

Ok here is what I found, in the URL System Programming Guide:

In both iOS and OS X, when the user relaunches your app, your app should immediately create background configuration objects with the same identifiers as any sessions that had outstanding tasks when your app was last running, then create a session for each of those configuration objects. These new sessions are similarly automatically reassociated with ongoing background activity.

So once I create the session again and hook up the delegate, the delegate methods start firing again, and I use getTasksWithCompletionHandler to see if there were any tasks in progress and return this to my UI and the rest of the system

这篇关于NSURLSession从崩溃恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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