3

I've been following a youtube tutorial for using LazyVStacks. https://www.youtube.com/watch?v=o6D7mUXjSmI

When I run the same code as per the tutorial, the LazyVStack using Xcode Version 13.2.1 (13C100) on a iPhone 11 Pro Max (running iOS 15.2) prints out 83 statements, when only 42 rows are in view. As per the tutorial it should only print 42 statements to the console.

Is this an Xcode/iOS bug? I'm unable to download the latest Xcode as my Mac doesn't support macOS 12.0 to verify.

Video tutorial showing on 42 print statements in the console on iPhone 11 Pro Max: Video tutorial showing on 42 print statements in the console on iPhone 11 Pro Max

My code showing 83 print statements in the console on iPhone 11 Pro Max My code showing 83 print statements in the console on iPhone 11 Pro Max

import SwiftUI

struct SampleRow: View {
    let id: Int

    var body: some View {
        Text("Row \(id)")
    }
    
    init(id: Int) {
        print("Loading row \(id)")
        self.id = id
    }
}

struct ContentView: View {
    var body: some View {
        ScrollView {
            LazyVStack {
                ForEach(1...1000, id: \.self, content: SampleRow.init)
            }
        }
    }
}
7
  • LazyVStacks will instantiate the number of views the OS thinks it may need based on a number of factors. This is part of the reason that you can't use .onAppear() to determine if a view is actually on screen. There is always a balance, that we don't control, between usage of resources and fluidity of scrolling. In other words, this is not a surprising result. Commented Apr 9, 2022 at 15:05
  • This goes against the purpose of the LazyVStack and the Apple documentation which states "The stack is “lazy,” in that the stack view doesn’t create items until it needs to render them onscreen. developer.apple.com/documentation/swiftui/lazyvstack Commented Apr 11, 2022 at 11:31
  • Agreed, but the definition of "needs to render them" is creation prior to rendering on screen so they can have a smooth scroll. The faster the scrolling, the further ahead the OS will get. If it is not lazy, it simply instantiates all of them. Commented Apr 11, 2022 at 14:01
  • So why does the tutorial show 42 print statements, but mine shows 83, for the same code and same device? The behaviour seems inconsistent and I don’t understand why. Commented Apr 11, 2022 at 16:39
  • Because it depends upon many factors, including the type of devices. Also, are you using the exact same versions of Xcode and the operating system? You should not get hung up on things you cannot control. This is controlled by the OS, and there is nothing we as developers can do to alter it. You have 83 out of 1000. That is lazy instantiation. Commented Apr 11, 2022 at 16:47

2 Answers 2

1

It's working fine in Xcode 13. I'm not sure why you need a LazyVStack, unless you're using some sort of grid. I slightly modified the code based on there's no much use for it here. It's printing all 1000.

print

enter image description here

import SwiftUI

struct SampleRow: View {
    let id: Int

    var body: some View {
        Text("Row \(id)")
    }
    

    init(id: Int) {
        print("Loading row \(id)")
        self.id = id
    }
}

struct ContentView: View {
    var body: some View {
        ScrollView {
            ForEach(1...1000, id: \.self) { index in
                SampleRow(id: index)
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Regardless of whether the LazyVStack is needed here, it doesn't seem to be doing what it has been designed to as per the Apple documentation which states "The stack is “lazy,” in that the stack view doesn’t create items until it needs to render them onscreen." The comparison of what the tutorial shows vs what I get shows this doesn't it? developer.apple.com/documentation/swiftui/lazyvstack
I don’t understand the question then? What’s the end goal?
For the same code in the tutorial, run on the same simulator device, why do I get 83 print statements printed to the console vs 42 print statements in the tutorial
Maybe the tutorial is outdated. Have you tried the code sample. I adjusted the code slightly.
Have you actually ran my code above to see what happens for you? How many print statements do you get?
|
1

I got the exact same problem, I've got several views that render and fetch data dynamically upon reaching the bottom of the page but for some unknown reason, the same exact code is not working on a view where all the data are rendered at once.. I don't know if it's a bug in LazyVStack implementation or anything else but the behavior is ambiguous

1 Comment

I have the same exact problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.