当前位置:  开发笔记 > 编程语言 > 正文

Alamofire网络调用不在后台线程中运行

如何解决《Alamofire网络调用不在后台线程中运行》经验,为你挑选了1个好方法。

据我所知,默认情况下,Alamofire请求在后台线程中运行.

当我尝试运行此代码时:

let productsEndPoint: String = "http://api.test.com/Products?username=testuser"

        Alamofire.request(productsEndPoint, method: .get)
            .responseJSON { 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)

        }

在网络呼叫的所有项目都已完成打印之前,UI没有响应; 所以我尝试将GCD与Alamofire一起使用:

let queue = DispatchQueue(label: "com.test.api", qos: .background, attributes: .concurrent)

    queue.async {

        let productsEndPoint: String = "http://api.test.com/Products?username=testuser"

        Alamofire.request(productsEndPoint, method: .get)
            .responseJSON { 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)

        }
    }

用户界面仍然没有像以前那样反应迟钝.

我在这里做错了什么,或者Alamofire的错误是什么?



1> SJCypher..:

关于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)
     }


我认为Alamofire请求默认在后台线程上运行,响应处理程序在主线程上运行.请注意,因为现在看来你正在后台线程上运行你的响应处理程序(如果你正在进行任何更改到UI你必须切换到主线程)
推荐阅读
ifx0448363
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有