У меня есть функция, делающая несколько HTTP-запросов с Alamofire.Я хочу дождаться, пока все они закончат, чтобы вернуть значение.Тем не менее, он застревает в dispatch.wait ()
class func getActionField(fieldid: String, completion: @escaping (_ res: [String: [Double]]) -> Void) {
var resreturn: [String: [Double]] = ["temperature":[], "humidity":[], "ph":[], "light":[]]
let dispatch = DispatchGroup()
dispatch.enter()
Alamofire.request(url + "aktionsdaten/temperatur/" + fieldid, method: .get).responseJSON{ response in
resreturn["temperature"] = response.result.value as! NSArray as? [Double] ?? [0.0,0.0]
dispatch.leave()
}
dispatch.enter()
Alamofire.request(url + "aktionsdaten/light/" + fieldid, method: .get).responseJSON{ response in
resreturn["light"] = response.result.value as! NSArray as? [Double] ?? [0.0,0.0]
dispatch.leave()
}
dispatch.enter()
Alamofire.request(url + "aktionsdaten/ph/" + fieldid, method: .get).responseJSON{ response in
resreturn["ph"] = response.result.value as! NSArray as? [Double] ?? [0.0,0.0]
dispatch.leave()
}
dispatch.enter()
Alamofire.request(url + "aktionsdaten/feuchtigkeit/" + fieldid, method: .get).responseJSON{ response in
resreturn["humidity"] = response.result.value as! NSArray as? [Double] ?? [0.0,0.0]
dispatch.leave()
}
dispatch.wait()
completion(resreturn)
}