[swift-users] [Swift3][Linux][URLSession]: Core dump when trying to make simple HTTP request

Steven Harms sgharms at stevengharms.com
Thu Oct 27 07:01:58 CDT 2016


Hello Swift,

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("staring sync")
queue.async(execute: {
    print(">>> [\(queue.label)]: At the queue start")
    print("x")
    let task = session.dataTask(with: URL(string: "http://google.com")!,
completionHandler: {
        (data, response, error) in
        print("Task ran!")
    })
    print("y")
    task.resume()
    print("z")
})
print("ending sync")

With output:

staring sync
ending sync

or...

staring sync
ending sync
>>> [com.apple.root.background-qos]: At the queue start
x
y
y
z

Whoa.

At this point I'm going to have to confess that I need some help clarifying
my model.

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,

Steven

References:

[gist]: https://gist.github.com/stinger/71000c4d5fb4a25fb7686bae116df0a5
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161027/63ecaf84/attachment.html>


More information about the swift-users mailing list