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?
source share