2

I am generating Text in ForEach loop and using a Form to give a style, how ever it does not work in heavy data, I am trying to use LazyVStack in sow how but I was not successful! How we can load data to a Form with heavy data?

PS: the same code just working fine with ForEach and without using Form or List, even with heavier data with help of LazyVStack. I just want make codes work with using Form or List.

    struct ContentView: View
{
    
    var body: some View
    {
    Form
    {
        ForEach(0 ..< 1000000) { index in
            Text("Row \(index)")
        }

    }

    
    }

}
3
  • This is a purely theoretical question. I can't imagine loading 1 million rows in a Form is a valid use case (from both developer and UX perspective). I suggest you add a real-world example of the data you want to put there. Commented Nov 1, 2020 at 0:18
  • 1
    that is right it is theoretical, It is clearly a theoretical, but who cares? it works with LazyVStack, I just intrested to use Form Style, to give Style to my Texts Commented Nov 1, 2020 at 0:21
  • This is not a theoretical question. In practice it might be needed for example for a time series data when user can scroll forward/backward trough days as far as he want. Commented Sep 10, 2021 at 9:00

1 Answer 1

4

The problem is that static data source (and all internal dependencies) are allocated at once, even if later the corresponding UI rows will be shown only when needed.

Here is possible approach to solve this scenario - to make data container dynamic and load data by blocks (taking into account that List/Form reuses rows)

Tested with Xcode 12 / iOS 14

struct ContentView: View {
    let max = 1000000 // even Int.max, if you want
    let block = 10

    @State private var indices = [0]   // << initially loaded small part

    var body: some View {
        List {
            ForEach(indices, id: \.self) { index in
                Text("Row \(index)")
                    .onAppear {

                        // load next block when last row shown (or can be 
                        // tuned to preload in advance)

                        if indices.last == index && index < max {
                            let next = max - index
                            indices.append(contentsOf:
                                Array(index + 1..<index+(next > block ? block : next)))
                        }
                    }
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

How advanced is your code! WoW! even better than LazyVStack! And some of us coming and given negative to this question! I really impressed of your codes! thanks

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.