Challenge: Fetch Artwork over the Network |

This is a companion discussion topic for the original entry at

You said we didn’t have to put the UI code to main thread in SwiftUI since musicItems was a State variable.
Do we really need DispatchQueue.main.async closure here?

      //DispatchQueue.main.async {
        self.musicItems = response.results

I tried it worked well without DispatchQueue.main.async closure.

Yes, per that logic, we aren’t updating a user interface element. I’ll dig some more digging around, but I’m guessing you are right.

I noticed the same thing because I’d implemented the disabled button step too and forgotten to include that in the dispatch closure, but it worked by accident. I think maybe with the use of states and such with SwiftUI and Combine now, we’re getting some of that thread protection for free? ie. Do states/observed objects/etc already update the views in a thread safe way?

Also, (nitpicking) would it be more accurate to call the challenge fetching titles rather than fetching artwork? I was anticipating stuff for setting up for images, but that turned out to be overkill in the solution.

@bdmoakley Do you have any feedback about this? Thank you - much appreciated! :]

As has been pointed out above, the title of the lesson suggests the exercise would go a step further and involve fetching of (a selected item’s?) artwork but the only new thing it introduces over the previous lesson is JSON parsing.