URLSession Concurrency on Linux #2
diegolavalledev
announced in
Posts
Replies: 1 comment 1 reply
-
Bro, 2.5 years later and I find myself with this exact same issue. And on google I had to force the "Linux" keyword with quotes to even find this post. It's nice to finally get some closure (no pun intended) on the question, even if I hate the answer. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Originally published on diegolavalle.com.
Being in the situation of having to write Swift code that runs on both Mac and Linux makes you realize some of the subtle differences between these two.
For instance you can use
URLSession
on the open source toolchain but you need to importFoundationNetworking
instead of justFoundation
for it to work.Now when if comes to language features both implementations of the tool chain should be at par. This includes the new concurrency constructs introduced in Swift 5.5, meaning we can use
async
,await
and the rest of them.I decided to test this assertion by writing a tool that fetches comments from the GitHub discussions API which will eventually be rendered under blog posts like this one.
Effectively the function from the snippet above successfully compiles on both systems without warnings.
Unfortunately this does not extend to the framework's additions which take advantage of the new concurrency features. Take for instance the asynchronous function URLSession.data which on macOS just works.
The same call on Linux will give us a compiler error since that exact version of the function does not exist yet.
While I couldn't find any DocC-style documentation for the open source version of Foundation, I was able to verify the status of the API on this status page where it states that URLSession if mostly complete but that getting tasks and other functions remain unimplemented. This can ultimately be verified in the source code.
So all of this means we have an opportunity to try a different concurrency feature called continuations and combine it with the soon-to-be legacy data task function.
We can even keep both approaches and apply them conditionally based on our target.
The full implementation of the
getComments
function is part of my Website Data project and can be found here.Beta Was this translation helpful? Give feedback.
All reactions