Created
June 3, 2017 04:21
-
-
Save trylovetom/55c6cdfc64fca3f83a14b2c676e633e2 to your computer and use it in GitHub Desktop.
sync url session data task
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
// | |
// 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