Alamofire network calls are not made in the background thread

I understand that by default, Alamofire requests run in the background thread.

When I tried to run 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 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 user interface did not respond until all elements of the network call completed printing; so I tried using GCD with 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 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 user interface is still not responding as before.

Am I really doing something wrong, or is the mistake on Alamofire?

+5
source share
1 answer

I was mistaken that Alamofire runs on the background thread by default. By default, it runs in the main queue. I opened the question on the Alamofire Github page and was resolved here: https://github.com/Alamofire/Alamofire/issues/1922

The correct way to solve my problem was to indicate in which queue I want my request to be launched using the queue parameter in the .responseJSON (

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

)

This is the whole revised version of my code:

 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 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) } 
+8
source

Source: https://habr.com/ru/post/1263011/


All Articles