A protocol that provides advance warning of the data requirements for a collection view, allowing the triggering of asynchronous data load operations.


protocol UICollectionViewDataSourcePrefetching


You use a prefetch data source object in conjunction with your collection view’s data source to begin loading data for cells before the collectionView(_:cellForItemAt:) data source method is called.

Follow these steps to add a prefetch data source to your collection view:

  1. Create the collection view and its data source.

  2. Create an object that adopts the UICollectionViewDataSourcePrefetching protocol, and assign it to the prefetchDataSource property on the collection view.

  3. Initiate asynchronous loading of the data required for the cells at the specified index paths in your implementation of collectionView(_:prefetchItemsAt:).

  4. Prepare the cell for display using the prefetched data in your implementation of the collectionView(_:cellForItemAt:) data source method.

  5. Cancel pending data load operations when the collection view informs you that the data is no longer required in the collectionView(_:cancelPrefetchingForItemsAt:) method.

For more information, see UICollectionView.

Loading Data Asynchronously

The collectionView(_:prefetchItemsAt:) method is not necessarily called for every cell in the collection view. Your implementation of collectionView(_:cellForItemAt:) must therefore be able to cope with the following potential situations:

  • Data has been loaded via the prefetch request, and is ready to be displayed.

  • Data is currently being prefetched, but is not yet available.

  • Data has not yet been requested.

One approach that handles all of these situations is to use Operation to load the data for each row. You create the Operation object and store it in the prefetch method. The data source method can then either retrieve the operation and the result, or create it if it doesn’t exist. For further information about how you can use asynchronous programming models to achieve this desired behavior, see Concurrency Programming Guide.


Managing Data Prefetching

Prefetching Collection View Data

Load data for collection view cells before they are displayed.

func collectionView(UICollectionView, prefetchItemsAt: [IndexPath])

Tells your prefetch data source object to begin preparing data for the cells at the supplied index paths.


func collectionView(UICollectionView, cancelPrefetchingForItemsAt: [IndexPath])

Cancels a previously triggered data prefetch request.


Inherits From

See Also


protocol UICollectionViewDataSource

The methods adopted by the object you use to manage data and provide cells for a collection view.

class UICollectionViewDiffableDataSource

The object you use to manage data and provide cells for a collection view.

struct NSDiffableDataSourceSnapshot

A representation of the state of the data in a view at a specific point in time.