![]() ![]() If you inspect the type of your app’s root view body, you’ll notice that it contains the entire view hierarchy:Īlthough Text can never become visible, it’s still present in _ConditionalContent.Īccording to Thinking in SwiftUI, the benefit of encoding the entire view hierarchy instead of the just currently visible one is that it allows for more efficient diffing. The type of view body encodes the whole view subgraph. Views from this hierarchy have their own identities, and their lifetime corresponds to how long they are displayed onscreen. SwiftUI uses the rendering hierarchy to produce an actual drawing.It relies on the private AttributeGraph framework to represent view attributes and perform diffing. On every UI update, SwiftUI traverses the previous and new snapshots of the non-rendering hierarchy to calculate changes. SwiftUI creates it using bodys of the view structs that we provide. The non-rendering hierarchy is a definition of what needs to be displayed onscreen.From what I’ve learned from reflecting the SwiftUI binary, their corresponding names are DisplayList and ViewList: SwiftUI.ViewGraph manages a rendering and non-rendering hierarchy of views. The identity and lifetime of SwiftUI views are separate from the lifetime of structs that define them. Then SwiftUI performs rendering to reflect the changes. Instead, we modify the state, and a new view graph is calculated from the state. To update UI, we don’t mutate the view graph directly. SwiftUI uses this definition to create an appropriate rendering. To describe what’s displayed onscreen, we create a graph of views. In this article, we will explore the three phases of the SwiftUI view lifecycle.īefore we talk about lifecycle, we need to agree on what a view is. Understanding it is essential when building apps in SwiftUI. Each view undergoes a series of events from its birth to its death, which is referred to as a lifecycle. ![]()
0 Comments
Leave a Reply. |