iOS Platform & SwiftUI (50 Qs)•Part 1: SwiftUI Architecture, State & App Lifecycle (1 - 10)
What is the difference between `@StateObject` and `@ObservedObject`?
senior LevelLifecycleSystem Design
In Simple Words:
Both observe classes conforming to ObservableObject:
@StateObject(Owner): The view instantiates and owns the object. SwiftUI ensures the instance is created only once and survives view re-evaluations.@ObservedObject(Consumer): The view receives an already-created object from a parent view. If used to instantiate an object inside a view, it will be destroyed and recreated every time the parent view re-renders, wiping out local state!- Rule: Create with
@StateObject; pass down with@ObservedObject.