I have a method that returns a bool (defaulted to false) and uses Alamofire to call an API which return a string. However, as the API request is asynchronous, the method always completes and returns false before the async request is finished. How can I wait till the API call is 'actually' complete in order to return the correct result?
I've tried putting the return statement within the .success and .failure blocks but that causes an error
func validateCredentials() -> Bool{
var apiResult = false
Alamofire.request("http://myURL?pUser=\(usernameTextField.text!)&pPwd=\(passwordTextField.text!)&pCheckIt=y")
.responseString {
response in
switch response.result{
case .success:
apiResult = true
case .failure:
apiResult = false
}
}
ProgressHUD.show("Please wait...")
return apiResult . <==== Always return false
}