我正在使用alamorefire调用API,但我认为如果我将应用程序放在后台,调用将暂停,我不希望发生这种情况。我想要加载它,然后,当我将应用程序带到前台时,我可以在UI上使用所请求的数据。如何做到这一点?目前,我只是像这样做简单的请求:
Alamofire.request(url, method: .get, parameters: params, headers: header())
.responseJSON{response in switch response.result {
我已经尝试使用以下alamofire配置:
let configuration = URLSessionConfiguration.background(withIdentifier: "com.cmpny.myapp.background")
let manager = Alamofire.SessionManager(configuration: configuration)
manager.request(url, method: .get, parameters: params, headers: headers())
这会给我以下错误:
Request failed with error: Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=http://url, NSLocalizedDescription=cancelled, NSErrorFailingURLKey=http://url}
发布于 2016-10-10 00:07:37
为此,您应该使用UIBackgroundTaskIdentifier
。
苹果文档:
https://developer.apple.com/reference/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio
类似的问题:https://stackoverflow.com/a/31751337/1689376
希望这能有所帮助;)
https://stackoverflow.com/questions/39945538
复制相似问题