SwiftUI: @State and the Attribute Graph
@Stateis one of the many SwiftUI’s pillars that, once understood, we take for granted and use pretty much everywhere without a second thought. But what is@State? What’s happening behind the scenes?
The answer is that
@Statedoes not store its value in the struct. The struct holds only a thin token - a reference to a node in an external, long-lived graph maintained by the SwiftUI runtime.[…]
State in the Attribute Graph is owned by the view that declares it. Lifetime of the graph node is tied to the lifetime of that view’s identity in the hierarchy.
As is generally known, SwiftUI hands off some of its work to a private framework called AttributeGraph. In this article we will explore how SwiftUI uses that framework to efficiently update only those parts of an app necessary and to efficiently get the data out of your view graph it needs for rendering your app.
In this talk, we’ll look at the system that underpins SwiftUI: the attribute graph.
Previously:
- Making Friends With AttributeGraph
- SwiftUI Data Flow 2023
- The SwiftUI Render Loop
- Lifetime of State Properties in SwiftUI
- Gosh Darn SwiftUI