A protocol that provides advance warning of the data requirements for a collection view, allowing the triggering of asynchronous data load operations.
SDKs
- iOS 10.0+
- tvOS 10.0+
Framework
- UIKit
Declaration
protocol UICollectionViewDataSourcePrefetching
Overview
You use a prefetch data source object in conjunction with your collection view’s data source to begin loading data for cells before the collection data source method is called.The following steps are required to add a prefetch data source to your collection view:
Create the collection view and its data source.
Create an object that adopts the
UICollectionprotocol, and assign it to theView Data Source Prefetching prefetchproperty on the collection view.Data Source Initiate asynchronous loading of the data required for the cells at the specified index paths in your implementation of
collection.View(_: prefetch Items At:) Prepare the cell for display using the prefetched data in your implementation of the
collectiondata source method.View(_: cell For Item At:) Cancel pending data load operations when the collection view informs you that the data is no longer required in the
collectionmethod.View(_: cancel Prefetching For Items At:)
Note
The prefetch method is not necessarily called for every cell in the table view. See Loading Data Asynchronously for details on a suggested approach to loading data.
When configuring the collection view object, assign your prefetch data source to its prefetch property. For more information about how a collection view works, see UICollection.
Loading Data Asynchronously
The collection method is not necessarily called for every cell in the collection view. Your implementation of collection 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.