gpt4 book ai didi

ios - Alamofire 4 在后台完成请求

转载 作者:行者123 更新时间:2023-11-28 08:26:07 26 4
gpt4 key购买 nike

我正在使用 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}

最佳答案

为此,您应该使用 UIBackgroundTaskIdentifier

苹果文档:

https://developer.apple.com/reference/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio

类似问题: https://stackoverflow.com/a/31751337/1689376

希望这有帮助;)

关于ios - Alamofire 4 在后台完成请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39945538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com