Swiftify code in NSURLSession+Synchronous

This commit is contained in:
Denis Lebedev 2016-10-16 18:08:40 +01:00
parent 2ee32843cf
commit 516c009392

View file

@ -17,15 +17,15 @@ public extension URLSession {
/// Return data from synchronous URL request /// Return data from synchronous URL request
public static func requestSynchronousData(_ request: URLRequest) -> Data? { public static func requestSynchronousData(_ request: URLRequest) -> Data? {
var data: Data? = nil var data: Data? = nil
let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0) let semaphore = DispatchSemaphore(value: 0)
let task = URLSession.shared.dataTask(with: request, completionHandler: { let task = URLSession.shared.dataTask(with: request) {
taskData, _, error -> () in taskData, _, error -> () in
data = taskData data = taskData
if data == nil, let error = error {print(error)} if data == nil, let error = error {print(error)}
semaphore.signal(); semaphore.signal()
}) }
task.resume() task.resume()
let _ = semaphore.wait(timeout: DispatchTime.distantFuture) let _ = semaphore.wait(timeout: .distantFuture)
return data return data
} }
@ -37,18 +37,18 @@ public extension URLSession {
} }
/// Return JSON synchronous from URL request /// Return JSON synchronous from URL request
public static func requestSynchronousJSON(_ request: URLRequest) -> AnyObject? { public static func requestSynchronousJSON(_ request: URLRequest) -> Any? {
guard let data = URLSession.requestSynchronousData(request) else {return nil} guard let data = URLSession.requestSynchronousData(request) else {return nil}
return try! JSONSerialization.jsonObject(with: data, options: []) as AnyObject? return try! JSONSerialization.jsonObject(with: data, options: [])
} }
/// Return JSON synchronous from specified endpoint /// Return JSON synchronous from specified endpoint
public static func requestSynchronousJSONWithURLString(_ requestString: String) -> AnyObject? { public static func requestSynchronousJSONWithURLString(_ requestString: String) -> Any? {
guard let url = URL(string: requestString) else {return nil} guard let url = URL(string: requestString) else {return nil}
let request = NSMutableURLRequest(url:url) var request = URLRequest(url:url)
request.httpMethod = "GET" request.httpMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Content-Type")
return URLSession.requestSynchronousJSON(request as URLRequest) return URLSession.requestSynchronousJSON(request)
} }
} }