paololeonardi / WaterfallGrid

A waterfall grid layout view for SwiftUI.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nondeterministic layout bug

pocketpixels opened this issue · comments

Hi,
I am using the WaterfallGrid to layout thumbnails of different aspect ratios. The data (including the JPEG data for the thumbnail images) comes from a local database (using GRDB). But it is read synchronously on the main thread, and the order (and content) of the displayed items does not change.
However the layout does. Sometimes it displays correctly, sometimes it does not (thumbnails overlap). See attached video where I switch back and forth between tabs of the app (not an actual TabView, the tab content is recreated from scratch each time).

The following error/warning shows up in the Xcode console when the thumbnails end up overlapping, but not when the layout works correctly:

Bound preference ElementPreferenceKey tried to update multiple times per frame.

Any idea what is causing this, and how I could fix it?

Btw, as you can see in the video, the layout always animates on initial display. Is that by design? And can that be disabled, ideally without disabling animations during changes?

layout_bug.mov

I just realized that this is likely a duplicate of #52.

Maybe it is caused by a cycle in the view updates as discussed in this SO answer?

Looking at the code myself, I have noticed there is a race in the layout computation, since it uses DispatchQueue to delay updating the position and sizing: https://github.com/paololeonardi/WaterfallGrid/blob/master/Sources/WaterfallGrid/WaterfallGrid.swift#L32. That probably has something to do with the non-determinism we are seeing here.

Any solution for this? Is this project still maintained?

I ended up implementing my own Waterfall layout from scratch back then. At least for the functionality I needed it wasn't difficult at all.