[swift-users] [Swift3][Linux][URLSession]: Core dump when trying to make simple HTTP request
sgharms at stevengharms.com
Thu Oct 27 07:01:58 CDT 2016
I've been trying out Swift as a general utility language on the server
(replacing Ruby or Node). I'm trying to write a simple image retriever via
HTTP as a learning project.
Inspired by this [gist], I've written the following:
let queue = DispatchQueue.global(qos: .background)
let sessionConfiguration = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfiguration)
print(">>> [\(queue.label)]: At the queue start")
let task = session.dataTask(with: URL(string: "http://google.com")!,
(data, response, error) in
>>> [com.apple.root.background-qos]: At the queue start
At this point I'm going to have to confess that I need some help clarifying
1. How did point "y" get fired twice? Or how did it happen not at all?
2. How did my callback for dataTask *never* fire? Even if the connection
task *failed* the handler ought have fired, no?
3. I didn't want to bring this whole queue business into the picture, but
it appears that without it the program exits before the handler has a
chance to fire.
4. Changing to a queue.sync from queue.async consistently produces the
output I expect, but does not fire my completionHandler still.
5. Is there a canonical reference for doing this simplest of tasks?
Thanks for the help,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the swift-users