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
1 Comment RSS · Twitter · Mastodon
Learning Flutter and the whole you bag your state in a state object is very similar. Not sure why anyone who likes this model would pick SwiftUI when you can run Flutter anywhere.
I just shake my head when I look at hundreds of lines of UI spaghetti tied together tightly like shoelaces and you describe everything about your view in a gigantic list. and then...whoops I meant to wrap that in a Column at the top and that causes like an 800 line diff because the entire code underneath indents.
What was the name of that tool that generated XML files that described your UI automatically?