Last active
May 9, 2018 14:14
-
-
Save llinardos/cec68712c6bf24da20c70592876a4f5e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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