I am trying to do a synchronous request using Alamofire
. I have looked on Stackoverflow and found this question: making an asynchronous alamofire request synchronous.
I saw that the accepted answer uses completion
to make Alamofire
request synchronous but I cannot make it to work. This is my simplified code:
func loadData(completion: (Bool)) -> (Int, [String], [String], [String]){
Alamofire.request(url!, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { response in
switch(response.result) {
case .success(_):
if let JSON = response.result.value as! [[String : AnyObject]]!{
//Here I retrieve the data
}
completion(true)
break
case .failure(_):
print("Error")
completion(false)
break
}
}
return (numberRows, nameArray, ageArray, birthdayArray)
}
With this code I am getting an error when trying to make completion(bool value)
. The error that I am getting is the following:
Cannot call value of non-function type 'Bool'
I have tried using a lot of examples using completion to get the values synchronously (because I need to retrieve the data before to show it on a table and at the same time get the number of rows of that table) without success.
How can I use that completion to get a synchronous response?
Thanks in advance!