[swift-users] [Swift3][Linux][URLSession]: Core dump when trying to make simple HTTP request
Evtim Papushev
e.papushev at me.com
Thu Oct 27 08:47:23 CDT 2016
Hi Steven,
Do you run your project in Xcode Playgrounds or under Linux?
Generally processes are associated with a single thread, the main thread. Once it exits the process and all executing threads are terminated.
1. Queue.async only schedules a task to be executed, then continues with the rest of the statements. In your case it prints "ending sync”, then it reaches the end and the process terminates.
2. Queue.sync schedules the block for execution, but it also does not return until this block gets executed. So, it effectively pauses the main thread waiting for the task to complete.
In both cases, however, you have an implicit asynchrony as all URLSession tasks run asynchronously. Imagine that it internally calls someQueue.async(…)
There are a few different approaches that you might want to investigate.
1. Make explicit synchronization:
print("staring sync")
let group = DispatchGroup()
group.enter()
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!")
group.leave()
})
print("y")
task.resume()
print("z")
})
print("ending sync")
group.wait()
I would go further and note, that since all URLSession tasks are asynchronous, there is absolutely no need to use additional queues:
(I took the liberty to add a set of URLs)
let sessionConfiguration = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfiguration)
let urls = ["https://www.google.com", "https://www.apple.com"]
let group = DispatchGroup()
print("staring sync")
urls.forEach { url in
group.enter()
session.dataTask(with: URL(string: url)!, completionHandler: {
(data, response, error) in
print("Task ran!")
group.leave()
}).resume()
}
print("ending sync")
group.wait()
2. Investigate the use of dispatch_main()
https://developer.apple.com/reference/dispatch/1452860-dispatch_main
Best,
Evtim
> On Oct 27, 2016, at 15:01, Steven Harms via swift-users <swift-users at swift.org> wrote:
>
> 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 <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 <https://gist.github.com/stinger/71000c4d5fb4a25fb7686bae116df0a5>_______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20161027/cab94f82/attachment.html>
More information about the swift-users
mailing list