函数 getJsonData 完成后,我需要调用函数 fillFields 。
func getJsonData是一个异步任务,用于在服务器上获取URLRequest的数据。
func getAPIData() {
let initial = URL(string: "http://10.0.0.2/Blower/app/api/inicial.php")
DispatchQueue.main.async {
_ = URLSession.shared.dataTask(with: initial!) { (dados, requisicao, erro) in
if requisicao != nil {}
if let dados = dados {
do {
let json = try JSONSerialization.jsonObject(with: dados, options: []) as! [String: Any]
/*
*
*/
} catch {
print(erro as Any)
}
}
}.resume()
}
}
我怎么知道函数getAPIData是否完成?
答案 0 :(得分:0)
这样的任务完成时,您可以使用“完成”处理程序进行标识。
func getAPIData(complition:@escaping (AnyObject?, Error?) -> Void) {
let initial = URL(string: "http://10.0.0.2/Blower/app/api/inicial.php")
DispatchQueue.main.async {
_ = URLSession.shared.dataTask(with: initial!) { (dados, requisicao, erro) in
if requisicao != nil {}
if let dados = dados {
do {
let json = try JSONSerialization.jsonObject(with: dados, options: []) as! [String: Any]
complition(json as AnyObject, nil) // When Complete task
// Call next function Here
} catch {
print(erro as Any)
complition(nil, erro)
}
} else {
complition(nil, erro)
}
}.resume()
}
}
像这样打电话
self.getAPIData { (response,error) in
print(response) // Your response is here after complete task
}