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
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.
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.
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?