Beginning Networking with URLSession, Episode 7: Challenge: Fetch Data Over the Network | Kodeco

In this challenge episode you will put your existing knowledge about URLSession to the test in order to download and display data downloaded from the network.


This is a companion discussion topic for the original entry at https://www.kodeco.com/28450876-beginning-networking-with-urlsession/lessons/7

Do you need to wrap the url request in a Task? I didn’t do it and it worked just fine

Do you have a snippet of your usage compared to this episode? Without that bit of context it might not be an accurate answer to your question :slight_smile:

Hope you’re enjoying the course!

fetchMusic() is already called in a Task. I also wonder whether wrapping session.data(from: url) in a Task is necessary.

Hello :slight_smile:

Regarding your question, about wrapping session.data(from: url) in a task I imagine you are referring to the solved challenge.

In this scenario, using Task within the function may not be strictly necessary as the calling context already calls it in a task. If you were to call this method elsewhere, however, it could be beneficial to do so in a task within the function, but there’s no one-size-fits-all solution.

As for how we call the function within the button’s action, if I recall correctly Task is used there in order to avoid a compiler error about calling an async function in a non-concurrent fashion.

:slight_smile:

1 Like

The nested task within the fetchMusic function is unneeded as the action closure within the Button is in an async context. Wouldn’t it be better practice to let the consumer of the function be in an async context to have more flexibility / control of when you are / aren’t entering in a background thread?