Last active
May 3, 2017 14:30
-
-
Save axelrivera/7d4442b2242a508f7bcfe9098b3bb96c 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 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