is Observable better than ObservableObject?

Asked on 09/06/2024

1 search

In the context of SwiftUI, the choice between using Observable and ObservableObject depends on the specific needs of your application.

ObservableObject is a protocol that you can use to create a class that can be observed by SwiftUI views. When properties marked with @Published within an ObservableObject change, SwiftUI automatically updates the views that depend on those properties. This is useful for managing state that is shared across multiple views or when you need to perform more complex state management that involves reference types.

On the other hand, Observable is not a standard SwiftUI protocol. It might be a custom protocol or a concept from another library or context. In general, if you are working within the SwiftUI framework, ObservableObject is the recommended approach for managing observable state in a way that integrates seamlessly with SwiftUI's declarative view updates.

For more detailed information on state management in SwiftUI, you can refer to the session SwiftUI essentials (09:43) from WWDC 2024, which discusses various tools for state management, including ObservableObject, State, and Binding.