I'm trying to make async
requests on Swift using Alamofire's
completion handler
and DispatchSemaphores
. I need to get a response and then return it to another method, so basically this is my code
func customRequest(zipCode: String) -> Bool{
var response = false
let dispatchQueueProcess = DispatchQueue.global(qos: .userInitiated)
let semaphore = DispatchSemaphore(value: 1)
dispatchQueueProcess.async {
semaphore.wait()
apiRequest(zipCode: zipCode) { apiResponse in
if apiResponse != nil {
response = apiResponse
} else {
print("Server did not Response")
}
semaphore.signal()
}
}
return response
}
The problem is that request always returns false
because is not waiting until apiRequest
responds... Do you have any idea to fix this? Thank you so much!
Ps. apiRequest
returns "true"/"false"
Is your intention here to create a synchronous function that will block and wait until the async request completes?
If so, you're almost there but you need to change the Semaphore to start with 0 rather than 1 (decrementing from 1 to 0 won't stop execution, it needs to go negative), and you need to move the wait() call outside the closure, otherwise you aren't stopping the function from returning and instead would be blocking your closure from ever completing.
You might find that you don't actually want to make a synchronous function like this, because if you call this from the main queue you will freeze the UI until the response is received.
Edit: This example should copy-paste run in a Playground