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

Hi @magfer!
Your explanation sounds right! The problem is that in the tutorial lookAndFeelStore is declared as @ObservedObject.
You said that " I think the idea is to use StateObject s at the begging of dependencies and inject them as ObservableObject s to avoid retaining cycles.", but we can see in tutorial, that lookAndFeelStore is only declared as ObservableObject, but never as StateObject.
Could you comment on that?

@eseniya, it’s been two years since I did the tutorial :sweat_smile:. Reading at the comments, I guess if the lookAndFeelStore was declared as ObservableObject and the view using it wasn’t redrawn, so it wouldn’t reset it. That would be one explanation why it didn’t matter using ObservableObject and StateObject.

As soon as you understand the difference between these two property wrappers, it’s fine :books:.

1 Like