简体   繁体   English

从后台URLSession发布中获取响应

[英]Getting Response From in Background URLSession Post

I'm executing a post that could take a few seconds to come back and what to enable background mode. 我正在执行一个可能需要几秒钟才能回来的帖子以及启用后台模式的内容。 I'm trying to figure out if the app goes into the background while waiting on the post to return can I get the response somehow? 我正在试图弄清楚应用程序是否在等待帖子返回时进入后台我可以以某种方式获得响应吗?

let bgSessionConfig = URLSessionConfiguration.background(withIdentifier: identifier)
bgSessionConfig.sharedContainerIdentifier = "myid"
backgroundSessionManager = SessionManager(configuration: bgSessionConfig)

So basically will the completionHandler below ever get called if in the background? 因此,如果在后台,下面的completionHandler基本上会被调用吗?

sessionManager.request(url, method: .post, parameters: dataParameters, encoding: parameterEncoding, headers: finalHeaders)
            .validate()
            .responseJSON(queue: self.dispatchQueue, completionHandler: {response in

func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { }

The completion handler in handleEventsForBackgroundURLSession is not something that "gets called". handleEventsForBackgroundURLSession的完成处理程序不是“被调用”的东西。 It is something that you must call when urlSessionDidFinishEvents arrives. urlSessionDidFinishEvents到达时, 必须调用它。 What "gets called" is handleEvents itself. “被调用”是handleEvents本身。 Your job here is to specify the background URL session (possibly creating it if you were just launched) and make yourself its delegate, immediately. 您在这里的工作是指定后台URL会话(如果您刚刚启动,可能会创建它)并立即自己成为其委托。 That is how you will be contacted, through the normal delegate messages. 就是通过正常的委托消息联系您的方式。

You would have received certain delegate messages if this had not been a background session; 如果这不是后台会话,您将收到某些委托消息; well, now you will receive those delegate messages when this is a background session. 好吧,现在,当这一个后台会话时,您将收到这些委托消息。

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

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