Skip to content

Instantly share code, notes, and snippets.

@ole
Created February 22, 2018 00:08
Show Gist options
  • Save ole/efa4c2a36ec81eb09cc6d1d65df37beb to your computer and use it in GitHub Desktop.
Save ole/efa4c2a36ec81eb09cc6d1d65df37beb to your computer and use it in GitHub Desktop.
Paste this into a playground and let it run for 5 seconds. You’ll get a non-nil response *and* error.
import Foundation
import PlaygroundSupport
let bigFile = URL(string: "https://speed.hetzner.de/1GB.bin")!
let task = URLSession.shared.dataTask(with: bigFile) { (data, response, error) in
print("data: \(data)")
print("response: \(response)")
print("error: \(error)")
}
task.resume()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
print("Cancelling dataTask")
task.cancel()
}
PlaygroundPage.current.needsIndefiniteExecution = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment