<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Steven,<div class=""><br class=""></div><div class="">Do you run your project in Xcode Playgrounds or under Linux?</div><div class=""><br class=""></div><div class="">Generally processes are associated with a single thread, the main thread. Once it exits the process and all executing threads are terminated.</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">In both cases, however, you have an implicit asynchrony as all URLSession tasks run asynchronously. Imagine that it internally calls someQueue.async(…)</div><div class=""><br class=""></div><div class="">There are a few different approaches that you might want to investigate.</div><div class=""><br class=""></div><div class="">1. Make explicit synchronization:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">"staring sync"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span></b><span style="color: rgb(0, 0, 0);" class="">group</span><b class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> = </span><span style="font-variant-ligatures: no-common-ligatures" class="">DispatchGroup</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">()</span></b></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><b class="">group<span style="color: rgb(0, 0, 0); font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="color: rgb(62, 30, 129); font-variant-ligatures: no-common-ligatures;" class="">enter</span><span style="color: rgb(0, 0, 0); font-variant-ligatures: no-common-ligatures;" class="">()</span></b></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">queue</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">async</span><span style="font-variant-ligatures: no-common-ligatures" class="">(execute: {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">">>> [</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">\</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">queue</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">label</span><span style="font-variant-ligatures: no-common-ligatures" class="">)]: At the queue start"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"x"</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> task = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">session</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">dataTask</span><span style="font-variant-ligatures: no-common-ligatures" class="">(with: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">URL</span><span style="font-variant-ligatures: no-common-ligatures" class="">(string: </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"<a href="http://google.com" class="">http://google.com</a>"</span><span style="font-variant-ligatures: no-common-ligatures" class="">)!, completionHandler: {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> (data, response, error) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">"Task ran!"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><b class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span>group<span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">leave</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></b></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> })</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"y"</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> task.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">resume</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"z"</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">})</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">"ending sync"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><b class="">group</b><b style="color: rgb(62, 30, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">wait</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">()</span></b><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div></div><div class=""><br class=""></div><div class="">I would go further and note, that since all URLSession tasks are asynchronous, there is absolutely no need to use additional queues:</div><div class="">(I took the liberty to add a set of URLs)</div><div class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class=""><br class=""></span></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> sessionConfiguration = </span><span style="font-variant-ligatures: no-common-ligatures" class="">URLSessionConfiguration</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span><span style="font-variant-ligatures: no-common-ligatures" class="">default</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> session = </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">URLSession</span><span style="font-variant-ligatures: no-common-ligatures" class="">(configuration: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">sessionConfiguration</span><span style="font-variant-ligatures: no-common-ligatures" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> urls = [</span><span style="font-variant-ligatures: no-common-ligatures" class="">"<a href="https://www.google.com" class="">https://www.google.com</a>"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span><span style="font-variant-ligatures: no-common-ligatures" class="">"<a href="https://www.apple.com" class="">https://www.apple.com</a>"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">]</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><div style="margin: 0px; line-height: normal; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class=""><br class=""></span></div><div style="margin: 0px; line-height: normal; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(186, 45, 162);" class="">let</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> group = </span><span style="font-variant-ligatures: no-common-ligatures;" class="">DispatchGroup</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">()</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="color: rgb(209, 47, 27); font-variant-ligatures: no-common-ligatures;" class="">"staring sync"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">urls</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">forEach</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { url </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">group.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);" class="">enter</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">session</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">dataTask</span><span style="font-variant-ligatures: no-common-ligatures" class="">(with: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">URL</span><span style="font-variant-ligatures: no-common-ligatures" class="">(string: url)!, completionHandler: {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> (data, response, error) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">in</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">"Task ran!"</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">group.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">leave</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }).</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">resume</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(62, 30, 129);" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="color: rgb(209, 47, 27); font-variant-ligatures: no-common-ligatures;" class="">"ending sync"</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">group.</span><span style="color: rgb(62, 30, 129); font-variant-ligatures: no-common-ligatures;" class="">wait</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span><span style="font-variant-ligatures: no-common-ligatures" class=""></span></div></div><div class=""><br class=""></div><div class="">2. Investigate the use of <b class="">dispatch_main()</b> </div><div class=""><a href="https://developer.apple.com/reference/dispatch/1452860-dispatch_main" class="">https://developer.apple.com/reference/dispatch/1452860-dispatch_main</a></div><div class=""><br class=""></div><div class="">Best,</div><div class="">Evtim</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 27, 2016, at 15:01, Steven Harms via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hello Swift,<div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">Inspired by this [gist][], I've written the following:</div><div class=""><br class=""></div><div class=""><div class=""><font face="monospace, monospace" class="">let queue = DispatchQueue.global(qos: .background)</font></div><div class=""><font face="monospace, monospace" class=""><br class=""></font></div><div class=""><font face="monospace, monospace" class="">let sessionConfiguration = URLSessionConfiguration.<wbr class="">default</font></div><div class=""><font face="monospace, monospace" class="">let session = URLSession(configuration: sessionConfiguration)</font></div><div class=""><br class=""></div><div class=""><font face="monospace, monospace" class="">print("staring sync")</font></div><div class=""><font face="monospace, monospace" class="">queue.async(execute: {</font></div><div class=""><font face="monospace, monospace" class=""> print(">>> [\(queue.label)]: At the queue start")</font></div><div class=""><font face="monospace, monospace" class=""> print("x")</font></div><div class=""><font face="monospace, monospace" class=""> let task = session.dataTask(with: URL(string: "<a href="http://google.com/" target="_blank" class="">http://google.com</a>")!, completionHandler: {</font></div><div class=""><font face="monospace, monospace" class=""> (data, response, error) in</font></div><div class=""><font face="monospace, monospace" class=""> print("Task ran!")</font></div><div class=""><font face="monospace, monospace" class=""> })</font></div><div class=""><font face="monospace, monospace" class=""> print("y")</font></div><div class=""><font face="monospace, monospace" class=""> task.resume()</font></div><div class=""><font face="monospace, monospace" class=""> print("z")</font></div><div class=""><font face="monospace, monospace" class="">})</font></div><div class=""><font face="monospace, monospace" class="">print("ending sync")</font></div></div><div class=""><br class=""></div><div class="">With output:</div><div class=""><br class=""></div><div class=""><div class=""><font face="monospace, monospace" class="">staring sync</font></div><div class=""><font face="monospace, monospace" class="">ending sync</font></div></div><div class=""><br class=""></div><div class="">or...</div><div class=""><br class=""></div><div class=""><div class=""><font face="monospace, monospace" class="">staring sync</font></div><div class=""><font face="monospace, monospace" class="">ending sync</font></div><div class=""><font face="monospace, monospace" class="">>>> [com.apple.root.background-qos]: At the queue start</font></div><div class=""><font face="monospace, monospace" class="">x</font></div><div class=""><font face="monospace, monospace" class="">y</font></div><div class=""><font face="monospace, monospace" class="">y</font></div><div class=""><font face="monospace, monospace" class="">z</font></div></div><div class=""><br class=""></div><div class="">Whoa.</div><div class=""><br class=""></div><div class="">At this point I'm going to have to confess that I need some help clarifying my model.</div><div class=""><br class=""></div><div class="">1. How did point "y" get fired twice? Or how did it happen not at all?</div><div class=""><br class=""></div><div class="">2. How did my callback for dataTask *never* fire? Even if the connection task *failed* the handler ought have fired, no?</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">4. Changing to a queue.sync from queue.async consistently produces the output I expect, but does not fire my <font face="monospace, monospace" class="">completionHandler</font> still.</div><div class=""><br class=""></div><div class="">5. Is there a canonical reference for doing this simplest of tasks?</div><div class=""><br class=""></div><div class="">Thanks for the help,</div><div class=""><br class=""></div><div class="">Steven</div><div class=""><br class=""></div><div class="">References:</div><div class=""><br class=""></div><div class="">[gist]: <a href="https://gist.github.com/stinger/71000c4d5fb4a25fb7686bae116df0a5" target="_blank" class="">https://gist.github.<wbr class="">com/stinger/<wbr class="">71000c4d5fb4a25fb7686bae116df0<wbr class="">a5</a></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></body></html>