pizza7
pizza7

Reputation: 471

Does UICollectionViewDiffableDataSource work with UICollectionViewDataSourcePrefetching?

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

Upvotes: 9

Views: 1636

Answers (2)

islam XDeveloper
islam XDeveloper

Reputation: 500

To Add The DataSourcePrefetching

extension ProductsCollectionView: UICollectionViewDataSourcePrefetching {
    func collectionView(_ collectionView: UICollectionView, prefetchItemsAt indexPaths: [IndexPath]) {
        print("islam fetch index\(indexPaths)")
        for index in indexPaths {
            if index.row >= array.count - 2 && !isFetchingDatas {
                loadMasterFile()
            }
        }
    }
}

Upvotes: 0

simeon
simeon

Reputation: 4626

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

Upvotes: 3

Related Questions