Last active
June 15, 2016 19:18
-
-
Save zhxnlai/995620226b21d41407b2a0e9b138683d to your computer and use it in GitHub Desktop.
Async Task NSURL
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 NSURL : ThrowableTaskType { | |
public typealias ReturnType = (NSData, NSURLResponse) | |
public func action(completion: Result<ReturnType> -> ()) { | |
ThrowableTask<ReturnType> { | |
let session = NSURLSession(configuration: .ephemeralSessionConfiguration()) | |
let (data, response, error) = Task { callback in session.dataTaskWithURL(self, completionHandler: callback).resume()}.await() | |
guard error == nil else { throw error! } | |
return (data!, response!) | |
}.asyncResult(completion: completion) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment