Conversation
|
Nobody should use view-models in SwiftUI |
|
@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 |
|
@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. |
|
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? |
|
@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 |
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.