This is a companion discussion topic for the original entry at https://www.raywenderlich.com/10376245-networking-with-urlsession/lessons/7
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/10376245-networking-with-urlsession/lessons/7
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?
// https://www.raywenderlich.com/10376245-networking-with-urlsession/lessons/7
//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.
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.