iOS

Difference between ObservedObject and StateObject in SwiftUI


Introduction

In SwiftUI, both @ObservedObject and @StateObject contain property wrappers that are used in conformance to the ObservableObject protocol at views. However, they serve different purposes and have different use cases:

How @ObservedObject Wroks?

class MyModel: ObservableObject {  @Published var data: String = "" } struct ParentView: View {  @StateObject private var model = MyModel() // Owns MyModel instance  var body: some View {  ChildView(model: model) // Passes existing instance to ChildView } } struct ChildView: View {  @ObservedObject var model: MyModel // Observes existing MyModel instance  var body: some View {  Text(model.data)  } } 
  • Ownership: @ObservedObject is used when there is a need for a view to observe some instance of ObservableObject that actually belongs to another view or any other component.

 

  • No Initialization: You should use @ObservedObject when you need to pass some instance of the ObservedObject to the view, but don’t create a new one.

 

  • Lifecycle: The instance of the @ObservedObject does not have its own lifecycle, rather it is created, or rather its lifecycle is managed by its parent view or wherever it is created, that depends on the context. If for example the observable object is changed (if its @Published fields update) then the view is updated correspondingly.

 

How @StateObject Works?

class MyModel: ObservableObject {  @Published var data: String = "" } struct MyView: View {  @StateObject private var model = MyModel() // Creates and owns MyModel instance  var body: some View {  Text(model.data)  } }

 

  • Ownership: @StateObject – when a view generates and maintains an instance of an ObservableObject. It arranges its initialization and gives it attributes which keep the object alive during the time of view.
  • Initialization: That’s when you should use @StateObject : when you don’t get the observable object instance as a parameter, but instead create it new within the view.
  • Lifecycle: The lifecycle of the @StateObject is used dependent on the lifecycle of the view that it is created in. It means if the view is recreated the @StateObject is also recreated, which may not be desirable if the object holds some state info.

 

Performance

The performance with an @ObservedObject does matter when the View is recreated frequently with a heavy weight object, but not an issue when the @ObservedObject is not complex.

Conclusion

Apply @StateObject when editing the instance of the observable object within a view from the other view. On the contrary, there is @ObservedObject: it is used when it is observed that an instance of an observable object is retrieved from a parent view or other source. This distinction is effective when or with respect to the lifecycle and ownership of observable objects in SwiftUI applications.

 

 

Ready to transform your business with our technology solutions?   Contact Us today to Leverage Our iOS Expertise.


iOS

Related Center Of Excellence