Loading...

@StateObject vs @ObservedObject in SwiftUI

question swiftui swift
Ram Patra Published on August 31, 2024

In SwiftUI, both @StateObject and @ObservedObject are property wrappers used to manage state in your views, specifically when working with objects that conform to the ObservableObject protocol. However, they serve slightly different purposes and have different use cases. Here’s a breakdown:

@StateObject

  • Purpose: Use @StateObject when you want to own the state of an ObservableObject. It ensures that the object is created once during the view’s lifecycle and is retained by the view.
  • Usage: Typically, you use @StateObject in the view where the ObservableObject is first created. The view that owns the @StateObject will manage its lifecycle, including its creation, updates, and destruction.
  • Example:
    class MyViewModel: ObservableObject {
        @Published var count = 0
    }
    
    struct MyView: View {
        @StateObject private var viewModel = MyViewModel()
          
        var body: some View {
            Text("Count: \(viewModel.count)")
            Button("Increment") {
                viewModel.count += 1
            }
        }
    }
    

    In this example, MyView owns the MyViewModel instance, which will persist as long as MyView is alive.

@ObservedObject

  • Purpose: Use @ObservedObject when you want to observe an ObservableObject that is owned elsewhere. It allows a view to react to changes in an ObservableObject, but the view does not manage its lifecycle.
  • Usage: Typically used when you pass an existing ObservableObject down the view hierarchy. The object is created and owned by another view, and you’re just observing changes to it.
  • Example:
    struct ParentView: View {
        @StateObject private var viewModel = MyViewModel()
          
        var body: some View {
            ChildView(viewModel: viewModel)
        }
    }
    
    struct ChildView: View {
        @ObservedObject var viewModel: MyViewModel
          
        var body: some View {
            Text("Count: \(viewModel.count)")
        }
    }
    

    Here, ParentView owns the viewModel using @StateObject, and ChildView observes it using @ObservedObject. If ParentView goes away, the viewModel will be destroyed as it is not owned by ChildView.

Key Differences

  1. Ownership:
    • @StateObject: View owns the ObservableObject.
    • @ObservedObject: View does not own the ObservableObject; it observes an external one.
  2. Lifecycle Management:
    • @StateObject: Manages the lifecycle (creation, retention, and destruction) of the ObservableObject.
    • @ObservedObject: Depends on the lifecycle of an external ObservableObject.
  3. Use Cases:
    • @StateObject: Use in views where the object is first created or owned.
    • @ObservedObject: Use in views that just need to observe an existing object passed from elsewhere.

Practical Example

When you want to create a view model in a view and keep it alive as long as the view is alive, use @StateObject. When a child view is interested in reacting to changes in a view model that it doesn’t create, use @ObservedObject.

Understanding when to use @StateObject versus @ObservedObject is key to managing state effectively in SwiftUI applications.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

KeyScreen

Show keypresses on your screen.

ToDoBar

Your to-dos on your menu bar.

SimpleFill

Fill forms using your right-click menu.

IconSim

Preview your Mac app icons.

Ram Patra Published on August 31, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui March 29, 2024 How to group different style modifiers and reuse them across multiple SwiftUI views?

In SwiftUI, you can create custom view modifiers to encapsulate common styling configurations and reuse them across different views. Here’s how you can create and reuse a custom view modifier:

question swiftui macos September 29, 2025 Adding Tooltips to SwiftUI Views on macOS

Tooltips are a subtle but powerful way to improve usability on macOS. They give users extra context when they hover over buttons, text, or icons without cluttering the main UI. SwiftUI makes it easy to add tooltips — and also flexible enough to create custom ones when you need more control.

question swiftui macos September 4, 2024 How to execute some code before app termination in a macOS app using SwiftUI?

To run some code before app termination in a macOS app using SwiftUI, the correct approach would involve placing the termination logic within a view, such as the ContentView. Here’s how you can do it:

Like my work?

Please, feel free to reach out. I would be more than happy to chat.