top of page
Writer's pictureDi Nerd Apps

ObservableObject vs StateObject in SwiftUI: What's the Difference?

Updated: Mar 25, 2023

One of the most significant features of SwiftUI is its support for state management, which allows developers to manage the state of their views and data in a much more straightforward way than before.


Two important tools for state management in SwiftUI are ObservableObject and StateObject. In this article, we'll explore the differences between these two tools, how to use them, and when to use them.


Understanding ObservableObject


An ObservableObject is a protocol that defines an object that can be observed for changes. The protocol requires that the object must be a class, and it must have at least one property that is annotated with the @Published property wrapper. The @Published property wrapper allows the property to notify any observers of changes to its value. Here's an example:


class Person: ObservableObject {
    @Published var name: String = ""
}

In this example, we’ve defined a Person class that conforms to the ObservableObject protocol. We've also defined a name property that is annotated with the @Published property wrapper, which means that any changes to the nameproperty will automatically notify any observers of the Person object.


We can now use the Person object in our SwiftUI views and observe any changes to its properties using the @ObservedObject property wrapper. Here's an example:

struct ContentView: View {
    @ObservedObject var person = Person()

    var body: some View {
        VStack {
            Text("Hello, \(person.name)")
            TextField("Name", text: $person.name)
        }
    }
}

In this example, we’re using the @ObservedObject property wrapper to observe changes to the Person object's nameproperty. Whenever the name property changes, SwiftUI will automatically update the view to reflect the new value.





Understanding StateObject


A StateObject is a property wrapper that creates an object that is owned by the view and survives across the view's life cycle. The object is created lazily when it is first accessed, and it is retained by the view until the view is destroyed. Here's an example:

struct ContentView: View {
    @StateObject var person = Person()

    var body: some View {
        VStack {
            Text("Hello, \(person.name)")
            TextField("Name", text: $person.name)
        }
    }
}

In this example, we’re using the @StateObject property wrapper to create a Person object that is owned by the view. Whenever the view is destroyed, the Person object will be destroyed as well. We can use the @StateObject property wrapper when we need to create an object that is tightly coupled to the view and should be destroyed when the view is destroyed.


When to Use ObservableObject vs. StateObject


So, when should we use ObservableObject vs. StateObject in our SwiftUI apps? Here are some guidelines:

  • Use ObservableObject when you have an object that is not tightly coupled to a specific view and should be shared across multiple views.


  • Use StateObject when you have an object that is tightly coupled to a specific view and should be destroyed when the view is destroyed.




17 views0 comments

Comments


bottom of page