How to handle the states in which the app goes into foreground setting off a number of requests (around 3-4 minimum) because that information is required in the app, and then going to background?
如何处理应用程序进入前台的状态,设置多个请求(最少3-4个),因为应用程序中需要该信息,然后转到后台?
What I have tried is to use a RequestManager to suspend
the URLSessionDataTasks
when app goes into background and when app resumes, resume
those tasks again. But I don't see this working very well.
我试过的是当app进入后台时使用RequestManager暂停URLSessionDataTasks,当应用程序恢复时,再次恢复这些任务。但我认为这种方法效果不错。
Is there a standard way to go about this?
有没有标准的方法来解决这个问题?
1 个解决方案
#1
1
Suspending tasks won't work, because the session no longer exists if your app gets jettisoned for low memory.
暂停任务不起作用,因为如果您的应用程序因内存不足而被丢弃,则会话不再存在。
The most straightforward approach would be to use a download task in a background session, then read the resulting temporary file when it finishes downloading. Download and upload tasks in background sessions are the only types of tasks that can survive your app getting jettisoned while in the background because of memory pressure.
最直接的方法是在后台会话中使用下载任务,然后在完成下载时读取生成的临时文件。在后台会话中下载和上传任务是唯一可以在您的应用程序在后台因为内存压力而被抛弃的任务类型。
If you absolutely must avoid downloading while the app is in the background (why?), you could create a download task in either a foreground or background session, then stop the download tasks by calling cancelByProducingResumeData:
when your app gets backgrounded. You can later continue the request by calling downloadTaskWithResumeData:
.
如果您在应用程序处于后台时绝对必须避免下载(为什么?),您可以在前台会话或后台会话中创建下载任务,然后通过调用cancelByProducingResumeData来停止下载任务:当您的应用程序变为后台时。您可以稍后通过调用downloadTaskWithResumeData:继续请求。
There is a rather large caveat with that approach, though, which is that the resume data portions of the API are not nearly as well tested as the background downloading portions. Case in point: in every version of iOS 10 from the first beta until 10.2, support for resume data was completely broken. (There is a rather horrific workaround, in case you choose to go down that path.)
但是,使用这种方法存在一个相当大的警告,即API的恢复数据部分几乎没有像后台下载部分那样经过良好测试。例证:在从第一个测试版到10.2版的每个版本的iOS 10中,对简历数据的支持完全被破坏了。 (有一种相当可怕的解决方法,如果你选择走这条路。)
So I would recommend the first approach unless you have some contractual or legal obligation not to do so.
因此,我建议采用第一种方法,除非您有一些合同或法律义务不这样做。
#1
1
Suspending tasks won't work, because the session no longer exists if your app gets jettisoned for low memory.
暂停任务不起作用,因为如果您的应用程序因内存不足而被丢弃,则会话不再存在。
The most straightforward approach would be to use a download task in a background session, then read the resulting temporary file when it finishes downloading. Download and upload tasks in background sessions are the only types of tasks that can survive your app getting jettisoned while in the background because of memory pressure.
最直接的方法是在后台会话中使用下载任务,然后在完成下载时读取生成的临时文件。在后台会话中下载和上传任务是唯一可以在您的应用程序在后台因为内存压力而被抛弃的任务类型。
If you absolutely must avoid downloading while the app is in the background (why?), you could create a download task in either a foreground or background session, then stop the download tasks by calling cancelByProducingResumeData:
when your app gets backgrounded. You can later continue the request by calling downloadTaskWithResumeData:
.
如果您在应用程序处于后台时绝对必须避免下载(为什么?),您可以在前台会话或后台会话中创建下载任务,然后通过调用cancelByProducingResumeData来停止下载任务:当您的应用程序变为后台时。您可以稍后通过调用downloadTaskWithResumeData:继续请求。
There is a rather large caveat with that approach, though, which is that the resume data portions of the API are not nearly as well tested as the background downloading portions. Case in point: in every version of iOS 10 from the first beta until 10.2, support for resume data was completely broken. (There is a rather horrific workaround, in case you choose to go down that path.)
但是,使用这种方法存在一个相当大的警告,即API的恢复数据部分几乎没有像后台下载部分那样经过良好测试。例证:在从第一个测试版到10.2版的每个版本的iOS 10中,对简历数据的支持完全被破坏了。 (有一种相当可怕的解决方法,如果你选择走这条路。)
So I would recommend the first approach unless you have some contractual or legal obligation not to do so.
因此,我建议采用第一种方法,除非您有一些合同或法律义务不这样做。