SwiftUI Data Flow 2023
Discover Observation in SwiftUI:
Simplify your SwiftUI data models with
Observation
. We’ll share how theObservable
macro can help you simplify models and improve your app’s performance. Get to knowObservation
, learn the fundamentals of the macro, and find out how to migrate fromObservableObject
toObservable
.
With the introduction of Swift macros, the SwiftUI team was able to reduce the number of property wrappers need to send data around, and remove a lot of boilerplate code.
For this article, I have re-written my sample app as a Mac app and updated it to use the new data macros.
[…]
Apart from adding some details to the decisions points, there are really only two additions to my chart:
- If a property doesn’t need to change, it can be a
let
.@Bindable
only works for classes. The equivalent for structs or primitive data types is still@Binding
.[…]
And lastly, in
@Observable
classes, everything that is NOT private is published. This is the opposite to what we had before where you had to explicItly state which properties were published.
[
Observable
] has a number of benefits:
- Simplified data flow, using
State
andEnvironment
. You no longer needStateObject
orEnvironmentObject
.- Less boiler-plate. No need to annotate properties with
@Published
or models with@ObservedObject
.- A view is now only updated when a property it depends on changes not when any change happens to the observable object.
- You can have arrays of observable models or even observable types that contain other observable types.
Apple has a useful guide on the steps to migrate an ObservableObject to an Observable. To summarise:
In case anyone else runs across this… Using
@Observable
on a class that has a property that is a closure causes the Xcode beta 3 compiler to flip out. Using@ObservationIgnored
on the said property will get the compiler to calm down.
Previously:
Update (2023-07-26): Keith Harrison:
In iOS 17, Apple deprecated the
onChange(of:perform)
view modifier replacing it with two new variations.
Update (2023-08-10): Natascha Fadeeva:
When it comes to state management in SwiftUI combined with reference types, SwiftUI provides two property wrappers: @StateObject and @ObservedObject. Understanding the difference between them is crucial for building robust SwiftUI applications, as it determines how data is managed and flows through the view hierarchy.
Note: Apple introduced the Observation framework at WWDC23 which will make these two property wrappers obsolete in the future. The knowledge around them is still useful since they are used in many existing applications. The knowledge might also help migrate to new Observation framework.
Update (2023-08-22): Thomas Clement:
Things are getting a bit weird in the swift forums, there’s been a huge amount of feedback in several discussions over several months about missing didSet support in Observability and Apple’s response has been just complete silence.
Update (2024-02-14): Donny Wals:
In this post, we’ll explore the new
@Observable
macro, we’ll explore how this macro can be used, and how it compares to the old way of doing things withObservableObject
.
Update (2024-08-23): Natalia Panferova:
As iOS 18 approaches and we might consider dropping support for versions below iOS 17 soon, I thought that now is a great time to explore how we can use Observation in our SwiftUI projects.
Update (2024-09-18): Jesse Squires (Mastodon):
SwiftUI’s new
@Observable
macro is not a drop-in replacement forObservableObject
. I learned of a subtle difference in behavior the hard way. Hopefully, I can save you from the same headache I experienced and save you some time.
Previously: