Skip to content

Instantly share code, notes, and snippets.

@trylovetom
Created June 3, 2017 04:21
Show Gist options
  • Save trylovetom/55c6cdfc64fca3f83a14b2c676e633e2 to your computer and use it in GitHub Desktop.
Save trylovetom/55c6cdfc64fca3f83a14b2c676e633e2 to your computer and use it in GitHub Desktop.
sync url session data task
//
// main.swift
// test
//
// Created by 張子晏 on 2017/6/3.
// Copyright © 2017年 張子晏. All rights reserved.
//
import Foundation
let semaphore = DispatchSemaphore(value: 0)
let url = URL ( string: "https://www.apple.com" )!
let request = URLRequest ( url: url )
let task = URLSession.shared.dataTask ( with: request ) { ( data, response, error ) in
let httpResponse = response as! HTTPURLResponse
let statusCode = httpResponse.statusCode
if (statusCode == 200) {
print("Everyone is fine, file downloaded successfully.")
} else {
print("Failed")
}
semaphore.signal() // releasing the resource
}
task.resume()
semaphore.wait() // requesting the resource
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment