I have ViewModel with ObservedObject conformance. One @Published variable is not used in view. Will its change cause view render?

Asked on 09/03/2024

1 search

In SwiftUI, a view's body is only re-evaluated when a property that it depends on changes. If you have a @Published variable in your ViewModel that is not used in the view's body, changes to that variable will not cause the view to re-render. SwiftUI creates dependencies only on the specific properties that are used within the view's body. This means that if a property is not read or used in the view, it does not establish a dependency, and changes to it will not trigger a re-render of the view.

This behavior is part of SwiftUI's efficient state management system, which ensures that views are only updated when necessary, based on the data they actually depend on. For more details, you can refer to the SwiftUI essentials session.

I have ViewModel with ObservedObject conformance. One @Published variable is not used in view. Will its change cause view render? | Ask WWDC