, Спасибо, Фидель, за разъяснение re: обработчик ответов.
лько я понимаю, по умолчанию запросы 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)
}
Пользовательский интерфейс не отвечал, пока все элементы из сетевого вызова не закончили печать; поэтому я попытался использовать 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)
}
}
и пользовательский интерфейс все еще не отвечает, как это было раньше.
Я здесь что-то не так делаю или виновата Аламофир?