observation observe inside viewModel

Asked on 06/16/2025

2 searches

It seems you're interested in how observation works within a ViewModel, particularly in the context of SwiftUI or UIKit. At WWDC, Apple introduced several enhancements related to observation tracking in both SwiftUI and UIKit.

In the session titled "What’s new in UIKit," automatic observation tracking was discussed. UIKit now supports automatic observation tracking, which allows for dependencies to be automatically established on any observable objects used within a handler. This means that any changes to the model's properties will automatically trigger updates to the UI without requiring extra code. You can learn more about this in the What’s new in UIKit session.

In SwiftUI, observation is also a key feature. The session "Better together: SwiftUI and RealityKit" explains how SwiftUI uses observation to track changes in data and update views accordingly. SwiftUI automatically calls a view's body again when an observable property changes, ensuring that the UI stays in sync with the underlying data. This is particularly useful when working with observable entities in RealityKit, as discussed in the Better together: SwiftUI and RealityKit session.

If you are looking to optimize performance, the session "Optimize SwiftUI performance with Instruments" provides insights into how to manage data dependencies more granularly to ensure that only necessary view bodies are updated when data changes. This can be explored further in the Optimize SwiftUI performance with Instruments session.

These sessions provide a comprehensive overview of how observation is integrated into both UIKit and SwiftUI, allowing for efficient and automatic UI updates in response to data changes.