NSURLSession resume from crash

情到浓时终转凉″ 提交于 2020-01-06 17:26:46

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/27734145/nsurlsession-resume-from-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!