mirror of
https://github.com/rensbreur/SwiftTUI.git
synced 2026-04-03 00:18:31 -06:00
Add Observation #7
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
ObservableObjectinfrastructure.Platform Availability
Since
Observationis 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 runin 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. Runtail -f /tmp/swift_tui_logto see the logs. Notice that theObservationViewon the left will only invalidate a node (trigger a view update) when the counter is showed. But theCombineViewon 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.