Alamofire: Network calls not being run in background thread

Created on 18 Jan 2017  ·  4Comments  ·  Source: Alamofire/Alamofire

I know that Alamofire does network calls by default on a background process, so I tried running this code:

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)`

The GET request is made perfectly, however, the UI is unresponsive; so I tried using GCD and putting the GET request on a background thread:

let queue = DispatchQueue(label: "com.test.com", 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)

        }
    }

and the UI is still unresponsive

Most helpful comment

My guess is the response handling is what's blocking the UI. By default the response handler is run on the main queue. Try setting your custom queue in the response parameter:

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

All 4 comments

My guess is the response handling is what's blocking the UI. By default the response handler is run on the main queue. Try setting your custom queue in the response parameter:

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

@ejensen Thank you. That solved my issue.

@cnoon @ejensen Hi , I have issue with queue passing in response

let serialQueue = DispatchQueue(label: "name", qos: .background)

Alamofire.request(request)
.responseJSON(queue: serialQueue){ response in

//this block not fire after pass queue
}
any idea,
Thank you

Same thing as @girishghoda 😕

Was this page helpful?
0 / 5 - 0 ratings