TL;DR: Is there some parameter or way to set the offset at which LazyVStack initialises views?
LazyVStack initialises the views lazily, so when I scroll, the next (few?) views are initialised.
I am loading an image once a view is drawn, using SDWebImage Package in swift. This takes a view milliseconds, and since I am using a LazyVStack, if one scrolls fast (even within reasonable limits), the placeholder is visible for a short moment, because the view has just been created a (too) short moment ago. If I scroll very slowly, the image loads just before the view appears, so no placeholder is visible.
If I could make the LazyVStack initialise the views just a few milliseconds earlier my problem would be gone…
Once would think this is a pretty common problem, timing this initialisation just right so as not to load too early or too late.. but nothing at all in the docs about this
3
Answers
Quick answer to the question: no
That being said, in this case there is still a solution: Since I was using
SDWebImageSwiftUI
before, simply calling the following already before the view starts to initialise solved my problem:then in my LazyVStack I use:
this process is called as
prefetching
-because you’re prefetching them so it will look smooth-And sorry, but there’s no way to access prefetching of
LazyVStack
in SwiftUI right now. Also, keep in mind that both SwiftUI’sGrid
AndLazyH/VStack
is not performant asUIKit
‘sUICollectionView
. So what you could do here is you can useUICollectionView
‘sUICollectionViewDataSourcePrefetching
protocol in your collection view’s data.I used
SDWebImage
Library to Fetch Images from internet (one of the most popular libraries forUIKit
)I tried to explain everything as comments in the code so give your attention to them, here’s what it looks like:
here’s the code:
and Here’s how you can implement it to swiftui:
I used lorem picsum which is a website for generating random images and that’s why you see images reloading randomly in my sample(that white ones), in your case, this shouldn’t be a problem
You can try adding extra space to the
ScrollView
and removing it using.padding
:PS: This method adds a bug with pull to refresh, but this bug is easy to fix, using a custom pull to refresh implementation 😊