WaterfallGrid
WaterfallGrid copied to clipboard
Performance problem
i see your code
return ScrollView(style.scrollDirection) {
ZStack(alignment: .topLeading) {
ForEach(data, id: self.dataId) { element in
self.content(element)
.frame(width: self.style.scrollDirection == .vertical ? columnWidth : nil,
height: self.style.scrollDirection == .horizontal ? columnWidth : nil)
.background(PreferenceSetter(id: element[keyPath: self.dataId]))
.alignmentGuide(.top, computeValue: { _ in self.alignmentGuides[element[keyPath: self.dataId]]?.y ?? 0 })
.alignmentGuide(.leading, computeValue: { _ in self.alignmentGuides[element[keyPath: self.dataId]]?.x ?? 0 })
.opacity(self.alignmentGuides[element[keyPath: self.dataId]] != nil ? 1 : 0)
}
}
.padding(style.padding)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.animation(self.loaded ? self.style.animation : nil)
}
i think your ForEach can change use List because when image count > 300 pic, use List can Recycle reuse
What do you think ??
List is UITableView. There is not UICollectionView alternative for SwiftUI. And i think there will be always performance problem with waterfall grid, because to define frame of 10_000th cell you must know frames of 9_999 previous cell. Think about 100_000, 1_000_000 and etc
You are right
@c941010623, I'm glad you brought up the performance topic.
There is indeed an issue with a large number of elements and @Elshad has already described one of the reasons behind it.
Also, the grid uses SwiftUI ZStack
which doesn't have a concept of reuse of the elements. All the grid views need to be created upfront and kept in memory even if not visible on screen.
For the kind of layout we want to achieve, the use of List
is not a solution here.
WaterfallGrid, ZStack, HStack and VStack play well with just a small/medium number of Views.
To be able to scale I believe we have to wait for a UICollectionView alternative for SwiftUI. But open for ideas.
Thank you both!
LazyVStack
could maybe be used when available to improve performance.