Skip to content

Add Observation#44

Open
DandyLyons wants to merge 1 commit intorensbreur:mainfrom
DandyLyons:main
Open

Add Observation#44
DandyLyons wants to merge 1 commit intorensbreur:mainfrom
DandyLyons:main

Conversation

@DandyLyons
Copy link
Copy Markdown

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.

@sisoje
Copy link
Copy Markdown

sisoje commented Mar 6, 2025

Nobody should use view-models in SwiftUI

@DandyLyons
Copy link
Copy Markdown
Author

@sisoje That is an uninformed and unhelpful comment. If you don't like view models, then don't use them. Regardless, view models are used by many others including Apple.

This library already has explicit support for view models via @ObservedObject, and now this PR provides feature parity for non-Apple platforms.

@sisoje
Copy link
Copy Markdown

sisoje commented Mar 7, 2025

@DandyLyons its the opposite, if you like view-models then you can use them inside your personal project. Swift/SwiftUI was not ment to be used like that, 90s are long gone. Nobody is using view-models in SwiftUI anymore, including Apple. Even if they did, they will fix that.
FYI Observation framework was added to support class semantics for the MODELS, it was not added for making view-models

Copy link
Copy Markdown

@sisoje sisoje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be too much

@btoms20
Copy link
Copy Markdown

btoms20 commented May 16, 2025

What's a good alternative to using @published and @ObservedObject? I'm actually curious, not being facetious. Like if we have a service doing work on a background thread that needs to periodically publish updates to the UI?

@Frank-III
Copy link
Copy Markdown

@sisoje I don't think your point is valid. even in the latest WWDC, they are demoing projects with MVVM structure, which I hate. this is a great blog post: https://dimillian.medium.com/swiftui-in-2025-forget-mvvm-262ff2bbd2ed and generally I think V-VM should be the future. (if you want opinions)

Also, if you don't like this approach, fork it and make it your opinionated library instead of commenting on others PR. not professional and not polite AT ALL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for Swift Observation

4 participants