Skip to content

Instantly share code, notes, and snippets.

@axelrivera
Last active May 3, 2017 14:30
Show Gist options
  • Save axelrivera/7d4442b2242a508f7bcfe9098b3bb96c to your computer and use it in GitHub Desktop.
Save axelrivera/7d4442b2242a508f7bcfe9098b3bb96c to your computer and use it in GitHub Desktop.
extension NSURLSession {
class func sendSynchronousRequest(request: NSURLRequest, inout returningResponse: NSHTTPURLResponse?, inout error: NSError?) -> NSData? {
var data: NSData?
var sem: dispatch_semaphore_t
sem = dispatch_semaphore_create(0)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (requestData, requestResponse, requestError) in
if let _requestError = requestError {
error = _requestError
}
if let _requestResponse = requestResponse {
returningResponse = _requestResponse as? NSHTTPURLResponse
}
if error == nil {
data = requestData
}
dispatch_semaphore_signal(sem)
}.resume()
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER)
return data
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment