0

A lot of sites online that demonstrate using a Lazy stack to load a lot of views use e.g. ForEach(1...100, id: \.self) { value in to demonstrate usage. But in a production app with complex views I can't really find any resources for where and how exactly to use Lazy Stacks. For example, I have a MasterView with many complex views as children (each View contains multiple charts which are expensive to draw). Should I use Lazy Stacks at the top level as I do below only, or should the subviews (ComplexViews) also use Lazy Stacks in them as well? Or should the subviews use LazyStacks and MasterView use a regular VStack? What would be most optimal from a performance (scrolling standpoint).

struct MasterView: View {
   
    var body: some View {
        NavigationView {
            ScrollView {
                LazyVStack {
                    ComplexView()
                    ComplexView()
                    ComplexView()
                    ComplexView()
                    ComplexView()
                    ComplexView() 
                }
            }
        }
    }
}

struct ComplexView: View {
    var body: some View {
       VStack { //LazyVStack?? 
            HStack { //LazyHStack?? 
              SwiftUIChart
              Text("Chart Data")
            }
             HStack {
              SwiftUIChart
              Text("Chart Data")
            }
             HStack {
              SwiftUIChart
              Text("Chart Data")
            }
            
       }
    }
}
GarySabo
  • 5,806
  • 5
  • 49
  • 124
  • The documentation states that a lazy stack doesn't render the item until it's going to be on screen. If that is useful behavior for you, use it. If not, skip it. https://developer.apple.com/documentation/swiftui/lazyvstack – jnpdx Dec 07 '21 at 19:36
  • @jnpdx yes but do you use Lazy Stacks all the way down to View Hierarchy, that is my question. – GarySabo Dec 07 '21 at 19:45
  • You use them wherever the behavior is beneficial. If you want something lazily-loaded in a stack, use it -- if you don't, you wouldn't use it. In your example above, if you want the chart data lazily loaded once the chart is on the screen, use the lazy stack. – jnpdx Dec 07 '21 at 19:45

0 Answers0