栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Alamofire网络通话未在后台线程中运行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Alamofire网络通话未在后台线程中运行

我对默认情况下在后台线程上运行Alamofire的做法有误。默认情况下,它实际上在主队列上运行。我已经在Alamofire的Github页面上打开了一个问题,并在这里解决了:https
:
//github.com/Alamofire/Alamofire/issues/1922

解决我的问题的正确方法是使用.responseJSON方法中的“ queue ”参数指定我希望在哪种队列上运行我的请求(

.responseJSON(queue: queue) { response in...}

这是我的代码的完整更正版本:

let productsEndPoint: String = "http://api.test.com/Products?username=testuser"    let queue = DispatchQueue(label: "com.test.api", qos: .background, attributes: .concurrent)    Alamofire.request(productsEndPoint, method: .get)        .responseJSON(queue: queue) { response in // check for errors guard response.result.error == nil else {     // got an error in getting the data, need to handle it     print("Inside error guard")     print(response.result.error!)     return } // make sure we got some JSON since that's what we expect guard let json = response.result.value as? [String: Any] else {     print("didn't get products as JSON from API")     print("Error: (response.result.error)")     return } // get and print the title guard let products = json["products"] as? [[String: Any]] else {     print("Could not get products from JSON")     return } print(products)     }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636419.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号