Bad Performance issue for LazyVStack , SwiftUI

4.7k views Asked by At

I have got a view with like 100 rows:

ForEach(moodEntries.reversed(), id: \.self) { entry in
                        Button(action: {
                            self.activeMoodForModal = moodEntries.lastIndex(of: entry) ?? 0
                            self.showMoodModal.toggle()
                        }) {
                            MoodTableViewCard(entry: entry)
                        }

                    }.sheet(item: $activeMoodForModal) { item in
                        MoodEntryModalView(entry: moodEntries[item], saveNote: {
                            do {
                                try context.save()
                            } catch {
                                print(error)
                            }
                        })
                    }

The performance is really bad: when I open this view the whole app freezes for 3 seconds on a simulator. If I change ForEach to List everything works fine so I suppose lazy loading will solve my problem. But I don't want to mess with List so I tried to put all the code mentioned above inside the LazyVStack { } but it didn't help: the code performs as bad as before. Is LazyVStack really so much worse than List or maybe I do something wrong?

1

There are 1 answers

1
Vivienne Fosh On

LazyVStack does not reuse cells at all, so no matter what you do - it will still lag on scrolling. If you have more elements than fit the screen and the layout more complex than Text("Hello") - use List.