8

My understanding is that UICollectionViewDataSourcePrefetching is built for UICollectionViewDataSource, whereas UICollectionViewDiffableDataSource doesn't seem to have any documentation around prefetching.

pizza7
  • 441
  • 4
  • 17

1 Answers1

3

Your implementation of UICollectionViewDataSourcePrefetching is set on a separate property of UICollectionView called prefetchDataSource

https://developer.apple.com/documentation/uikit/uicollectionview/1771768-prefetchdatasource

You will need to cache your prefetched data somewhere, and then access it from your cellProvider (or UICollectionView.CellRegistration). On your collection view you assign your prefetch data source to the prefetchDataSource property — this may just be your view controller e.g:

myCollectionView.prefetchDataSource = self

I've used this with UICollectionViewDiffableDataSource and the prefetch data source is consulted for the index paths to prefetch as the collection view is scrolled, while the actual data is supplied by the diffable data source

simeon
  • 4,466
  • 2
  • 38
  • 42
  • Any chance this is different between iOS 14 and 15? I've got a diffable data source set up on a list view in my app, and hooking up prefetching works on iOS 15 but does nothing on iOS 14. – Tom Hamming Feb 03 '22 at 19:04
  • @TomHamming It probably is different. It's the videos on iOS 15 that talk about using prefetching with a diffable data source. Some key changes were made under the hood. – matt Jul 03 '23 at 17:37