带有标识符的后台URLSession的问题 [英] Issues with A background URLSession with identifier

查看:205
本文介绍了带有标识符的后台URLSession的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中尝试使用此代码在后台下载大文件,并且工作正常.我开始在UIViewController中下载.如果我转到另一个视图并返回UIViewController,它将停止重新加载进度条.

I tried this code in my project to download large files in background and it works fine. I start downloading in UIViewController. If I go to another view and back to the UIViewController ,it stop reloading the progress bar.

我遇到了带有标识符的后台URLSession的问题!我想每次加载UIViewController时都创建一个新会话.关闭视图控制器时,我想使所有后台会话无效.我尝试了所有可能的方法来解决此问题,但没有成功.

I got the Issues with A background URLSession with identifier already exists!. I Want to create new session every time when UIViewController load. I want invalidate all background session when dismiss the view controller. I tried all possibilities to resolve this issue but don’t get succeed.

单击此处

推荐答案

创建后台会话时,您正在做两件事:

When you create a background session, you're doing two things:

  • 告诉nsurlsessiond(后台守护程序)以创建会话.
  • 在与该会话相关的应用中创建本地会话.

标识符的目的是允许您的应用在操作系统终止后重新连接到外部会话(由nsurlsessiond控制).

The purpose of the identifier is to allow your app to reconnect to the external session (controlled by nsurlsessiond) if your app gets terminated by the operating system.

据我所知,您的应用通常无法自动将其会话与后台会话分离.因此,当您的应用尝试创建具有相同标识符的会话时,突然有两个会话都试图在nsurlsessiond中与同一个外部会话进行对话,这会出错.这不是使用API​​的受支持方式.

To my knowledge, it is not normally possible for your app to voluntarily dissociate its session from the background session. So when your app tries to create a session with the same identifier, suddenly there are two sessions that are both trying to talk to the same external session in nsurlsessiond, and things go very wrong. That's not a supported way to use the API.

后台会话对象必须在您的应用程序运行的整个过程中一直保持活动状态.不要尝试在一次启动中处理它并重新创建它.除非您的应用重新启动,否则您永远不要创建具有相同ID的会话.

The background session object must be kept alive the entire time that your app is running. Don't try to dispose of it and recreate it within a single launch. You should not ever create a session with the same ID unless your app gets relaunched.

但是请注意,如果您的应用重新启动以处理后台事件(仅适用于iOS),则当您调用该事件提供的完成处理程序时,您应用的本地会话确实会失效,并且如果您的应用需要创建该本地会话在那之后做任何事情.这是我唯一知道的情况,后台会话在nsurlsessiond中永远不再与后台会话相关联,因此,这是唯一一次在一次启动中创建具有相同ID的会话两次的唯一情况(一次在您启动时被要求处理后台事件,然后当您接到didFinishLaunching调用以表明用户将您的应用程序置于前台时,可能会再次处理该事件.

Note, however, that if your app gets relaunched to handle background events (iOS only), when you call the completion handler provided by the event, your app's local session does get invalidated, and you need to create it if your app does anything after that. That's the only situation I'm aware of where a background session ever stops being associated with the background session in nsurlsessiond, and thus that's the only situation where you should ever create a session with the same ID twice in a single launch (once when you're asked to handle background events, and then potentially again when you get a didFinishLaunching call to indicate that the user foregrounded your app).

这篇关于带有标识符的后台URLSession的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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