SwiftUI Fundamentals, Episode 17: Challenge: StateObject & ObservableObject | Kodeco

In this hands-on challenge, practice everything you have learned about @StateObject and @ObservableObject, including which one to use for a given circumstance!


This is a companion discussion topic for the original entry at https://www.kodeco.com/37678937-swiftui-fundamentals/lessons/17

Why would you use @StateObject for the movieStore and @ObservedObject for the userStore/ lookAndFeelStore? Isn’t the movieList the “owner” of these properties as well? Is there a preference, as using @ObservedObject for the moviesStore and @StateObject for the userStore/lookAndFeelStore seem to work as well.

@hococoder I like to extend my thanks for the tutorial. I’d like to point out couple of things, though:

  1. Since this was a SwiftU fundamental, it would be nice if you slowed down and explained your challenges a bit better. You’re going too fast, and telling add this and that without the why is not good enough. Remember, we’re taking the course to learn something new, and a little bit more explanation on your challenge would’ve helped a lot to reinforce what we previously learned.
  2. it would also be nice to have the challenge written on a ReadMe file instead of just scattered all over the files and have me look at each file to see what you want me to do, or if a readme file is not possible, at least take 30 more seconds going over the files that we need to work on and explain it.

Thank you.

1 Like

In this exercise, it doesn’t make a big difference using either.
But overall, I think the idea is to use StateObjects at the begging of dependencies and inject them as ObservableObjects to avoid retaining cycles.

Also, if you only use an ObservableObject, this object gets re-instantiated for every View draw cycle triggered by any bound vars.

1 Like