I am making a web api call. I make the call from my class by calling the post method in my helper class. So i would like to return response i receive in the helper class(alamofire response) to the class the request is completed.
MyClass
let responseDictionary:[String:Any] = WebAPIHelper.postMethodRequest(parameters: requestParameters, methodName: "/User/SendEmail")
WebServiceHelper:
static func postMethodRequest(parameters:Parameters, methodName:String) -> [String:Any]
{
let headers:HTTPHeaders = generateHeaders(methodType: "POST", methodName: methodName)
var responseDictionary:[String: Any] = ["OperationStatus":"Success"]
Alamofire.request("http:acaapi.idyas.net" + methodName, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON{ response in
//This control comes to this block after a delay i.e., once the response is received.
responseDictionary = response.result.value as! [String: Any]
print(responseDictionary)
}
return responseDictionary // This line of code does not wait for the response to be received. Hence the control goes to MyClass before the response is received.
}
I want the method to return the responseDictionary only after the response is received. Can this be achieved with the help of Dispatchqueue or any other way to achieve this?
EDIT: Duncan: Pl try to understand the issue here. I want to return the response to another class. It is not just about calling a function inside the alamofire response block. I couldnt place the 'return responseDictionary' code inside the block. But what i need is, i want to return the responseDictionary after i receive the response.