Skip to content

Instantly share code, notes, and snippets.

@llinardos
Last active May 9, 2018 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save llinardos/cec68712c6bf24da20c70592876a4f5e to your computer and use it in GitHub Desktop.
Save llinardos/cec68712c6bf24da20c70592876a4f5e to your computer and use it in GitHub Desktop.
extension URLSession {
func sendSynchronousRequest(request: URLRequest) -> (Data?, URLResponse?, Error?) {
let semaphore = DispatchSemaphore(value: 0)
var result: (data: Data?, response: URLResponse?, error: Error?)
let task = self.dataTask(with: request) {
result = (data: $0, response: $1, error: $2)
semaphore.signal()
}
task.resume()
_ = semaphore.wait(timeout: DispatchTime.distantFuture)
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment