I'm about to create an extension for URLSession
in Swift 3 to create synchronous and asynchronous request. Here is my implementation
extension URLSession {
func sendSynchronousRequest(request: URL, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
let semaphore = DispatchSemaphore(value: 0)
let task = self.dataTask(with: request) { (data, response, error) in
completionHandler(data,response,error)
semaphore.signal()
}
task.resume()
semaphore.wait(timeout: .distantFuture)
}
func sendAsynchronousRequest(request: URLRequest, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
let task = self.dataTask(with: request) { data, response, error in
completionHandler(data, response, error)
}
task.resume()
return task
}
}
I have Xcode suggest me to insert the @escaping to the function. I don't know whether the implementation is correct
I also have warning at this line:
Anyone know how to correct the extension?