简体   繁体   English

在后台运行 HTTP 请求

[英]Run HTTP requests in the background

In my app, I need to send a sequence of 4 requests.在我的应用程序中,我需要发送一系列 4 个请求。 Requests need to be sent in order one after another.请求需要一个接一个地发送。

  • Request 1 - regular HTTP POST request to the backend请求 1 - 对后端的常规 HTTP POST 请求
  • Request 2 - file upload请求 2 - 文件上传
  • Request 3 - file upload请求 3 - 文件上传
  • Request 4 - regular HTTP POST request to the backend请求 4 - 对后端的常规 HTTP POST 请求

This sequence needs to work in the background.此序列需要在后台运行。 I know that on iOS there's URLSession.uploadTask method that allows uploading files in the background.我知道在 iOS 上有允许在后台上传文件的 URLSession.uploadTask 方法。

Let's say that the user starts the sequence of requests, and then background the app right away.假设用户启动了一系列请求,然后立即将应用程序置于后台。

  1. Can I make regular HTTP requests using URLSession.uploadTask method?我可以使用 URLSession.uploadTask 方法发出常规的 HTTP 请求吗?
  2. When URLSession.uploadTask will finish while the app is in the background and the delegate method当应用程序在后台和委托方法时 URLSession.uploadTask 将完成

func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?)

will be invoked, can I start another URLSession.uploadTask inside this delegate method, so that all 4 requests will be run in the background?将被调用,我可以在这个委托方法中启动另一个 URLSession.uploadTask,以便所有 4 个请求都将在后台运行吗?

  1. Is URLSession.uploadTask the right approach to run all 4 requests in the background? URLSession.uploadTask 是在后台运行所有 4 个请求的正确方法吗?

I will answer your all questions.我会回答你所有的问题。 But, firstly I choosed last.但是,首先我选择了最后。

  • 3-right approach to run all 4 requests in the background在后台运行所有 4 个请求的 3 正确方法
  • Yes, you can do with this way but It's missing some details.是的,你可以这样做,但它缺少一些细节。 For instance;例如; you start your requests sequentially, and after that your app goes to background.您按顺序启动请求,然后您的应用程序进入后台。 But for a while, if the user kill app.但有一段时间,如果用户杀死应用程序。 What will do for this scenario?这种情况会做什么?

  • 2-delegate method will catch in the background mode 2-delegate 方法将在后台模式下捕获
  • Answer You can proceed your request in background mode, and yes delegate methods will get any state changes.回答您可以在后台模式下继续您的请求,是的,委托方法将获得任何 state 更改。

  • 1-Can I make regular HTTP requests using URLSession.uploadTask method? 1-我可以使用 URLSession.uploadTask 方法发出常规的 HTTP 请求吗?
  • Answer Actually I prefer AFNetworking SDK for this issue.回答实际上,对于这个问题,我更喜欢 AFNetworking SDK。 I think, below topics will hep you to solve this issue.我认为,以下主题将帮助您解决此问题。

    • BGAppRefreshTask: An object representing a short task typically used to refresh content that's run while the app is in the background. BGAppRefreshTask:一个 object 代表一个短任务,通常用于刷新应用在后台时运行的内容。 Apple Document 苹果文档
    • Updating Your App with Background App Refresh使用后台应用刷新更新您的应用

Calling the completion handler in a timely manner, and with an accurate result, helps determine how much future execution time your app receives.及时调用完成处理程序并获得准确的结果,有助于确定您的应用程序接收的未来执行时间。 If you take too long to update your app, the system may schedule your app less frequently in the future to save power.如果您更新应用程序的时间过长,系统可能会在以后减少您的应用程序的调度频率以节省电量。

First, I would say you need to go to project settings, select your target, go to Signing and Capabilities and activate Background fetch and Background processing.首先,我想说你需要 go 来项目设置,select 你的目标,go 来签名和功能并激活后台获取和后台处理。

About the method to run the HTTP requests in the background, you can use that method or others.关于在后台运行 HTTP 请求的方法,您可以使用该方法或其他方法。 I personally recommend using Alamofire since it's the standard and has many features to expand your network layer in the future.我个人推荐使用 Alamofire,因为它是标准并且有许多功能可以在未来扩展您的网络层。

And yes, the delegate methods will still be executed in the background, after completion.是的,委托方法在完成后仍将在后台执行。 Make sure to set the delegate variable.确保设置委托变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM