Add Observation #7

Open
opened 2025-10-14 15:43:13 -06:00 by navan · 0 comments
Owner

Originally created by @DandyLyons on 10/30/2024

This PR adds support for the language-level Observation framework and also resolves #42.

This PR makes no changes to other functionality and does not affect the existing ObservableObject infrastructure.

Platform Availability

Since Observation is available on Windows/Linux/ other platforms, there is now a way to create observable view models on other platforms.

Future Opportunities for Backporting

Unfortunately Observation is only available on macOS 14+

The swift-perception library makes it quite easy to backport Observation to iOS 13+, macOS 10.15+ etc. If we were willing to add swift-perception as a dependency, it would be very easy to backport the observation feature.

Example

For demonstration purposes, this PR includes a new executable target named "SwiftTUIExample". (You may or may not want to add this to the Examples directory later.)

Simply run swift run in the terminal. This will build and run the "SwiftTUIExample" app. This app is a very simple side-by-side comparison of a View implemented with Observation and a View implemented with Combine.

Also for demonstration purposes I added a few log() calls to show exactly when Observation triggers View updates. Run tail -f /tmp/swift_tui_log to see the logs. Notice that the ObservationView on the left will only invalidate a node (trigger a view update) when the counter is showed. But the CombineView on the right will always invalidate a node even when the changed property is not currently affecting the view.

In other words, the Observation framework has the added benefit that it automatically reduces unnecessary View rerenders.

*Originally created by @DandyLyons on 10/30/2024* This PR adds support for the language-level [Observation framework](https://developer.apple.com/documentation/observation) and also resolves #42. This PR makes no changes to other functionality and does not affect the existing `ObservableObject` infrastructure. ## Platform Availability Since `Observation` is available on Windows/Linux/ other platforms, there is now a way to create observable view models on other platforms. ### Future Opportunities for Backporting Unfortunately Observation is only available on macOS 14+ The [swift-perception](https://github.com/pointfreeco/swift-perception) library makes it quite easy to backport Observation to iOS 13+, macOS 10.15+ etc. If we were willing to add swift-perception as a dependency, it would be very easy to backport the observation feature. ## Example For demonstration purposes, this PR includes a new executable target named "SwiftTUIExample". (You may or may not want to add this to the Examples directory later.) Simply run `swift run` in the terminal. This will build and run the "SwiftTUIExample" app. This app is a very simple side-by-side comparison of a View implemented with Observation and a View implemented with Combine. Also for demonstration purposes I added a few `log()` calls to show exactly when Observation triggers View updates. Run `tail -f /tmp/swift_tui_log` to see the logs. Notice that the `ObservationView` on the left will only invalidate a node (trigger a view update) when the counter is showed. But the `CombineView` on the right will always invalidate a node even when the changed property is not currently affecting the view. In other words, the Observation framework has the added benefit that it automatically reduces unnecessary View rerenders.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github/SwiftTUI#7
No description provided.