当前位置: 首页 > 面试题库 >

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

万俟震博
2023-03-14
问题内容

据我了解,默认情况下,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?


问题答案:

我对默认情况下在后台线程上运行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(基于Alamofire的下载管理器示例),我需要一些有关使用后台会话下载文件的说明。我需要重写SessionDelegate才能使其正常工作?还是只是? 通常,使用Alamofire在后台处理下载的步骤是什么?以及如何处理我的应用程序重新启动,下载量不断增加的情况。 问题答案: 更新资料 基于这个惊人的教程,我整理了一个GitHub上可用的示例项目。

  • 我查了一下,我怎么能默默地运行硒:在这里找到了下一个伟大的答案 我正在尝试让selenium正常运行,直到一些操作完成,然后在后台运行它。 有可能吗? 高级Oz中的thanx

  • 使用QQ轻游戏后台 QQ轻游戏后台用现成的房间逻辑,开发者可以使用进行房间创建、加入、离开、以及自带的帧同步以及消息同步方案。 使用QQ轻游戏后台详情跳转至此处 自建后台 使用自建后台,开发者可以使用引擎自带的 BK.WebSocket、BK.Socket、BK.HttpUtil三种方法进行后台数据的交换。 使用自建后台详情跳转至此处 因为自建后台的房间概念QQ轻游戏并不理解,为在聊天窗中模拟房间

  • 问题内容: 我想从主Java程序中产生一个Java线程,并且该线程应单独执行而不会干扰主程序。应该是这样的: 用户启动的主程序 做一些业务工作,应该创建一个可以处理后台进程的新线程 一旦创建线程,主程序就不要等到生成的线程完成。实际上,它应该是无缝的。 问题答案: 一种简单的方法是自己手动生成线程: 另外,如果您需要产生多个线程或需要重复执行,则可以使用更高级别的并发API和执行程序服务:

  • 问题内容: 我想在后台线程中运行一些Runnable。我想使用Handler,因为它便于延迟。我的意思是 凡 可运行 应当运行 后台 线程。是否可以创建这样的处理程序?是否在某个地方有“背景” Looper,或者该如何创建? PS我知道如何使用自定义类扩展Thread,但是比处理程序方式需要更多的编码工作。因此,请不要发布其他解决方案或类似的内容 如果Handler能以“干净”的方式做到这一点,我