NSCollectionViewDiffableDataSource
The object you use to manage data and provide items for a collection view.
Declaration
class NSCollectionViewDiffableDataSource<SectionIdentifierType, ItemIdentifierType> where SectionIdentifierType : Hashable, ItemIdentifierType : HashableOverview
A diffable data source object is a specialized type of data source that works together with your collection view object. It provides the behavior you need to manage updates to your collection view’s data and UI in a simple, efficient way. It also conforms to the NSCollectionViewDataSource protocol and provides implementations for all of the protocol’s methods.
To fill a collection view with data:
Connect a diffable data source to your collection view.
Implement an item provider to configure your collection view’s items.
Generate the current state of the data.
Display the data in the UI.
To connect a diffable data source to a collection view, you create the diffable data source using its init(collectionView:itemProvider:) initializer, passing in the collection view you want to associate with that data source. You also pass in an item provider, where you configure each of your items to determine how to display your data in the UI.
dataSource = NSCollectionViewDiffableDataSource<Int, UUID>(collectionView: collectionView) {
(collectionView: NSCollectionView, indexPath: IndexPath, itemIdentifier: UUID) -> NSCollectionViewItem? in
// configure and return item
}Then, you generate the current state of the data and display the data in the UI by constructing and applying a snapshot. For more information, see NSDiffableDataSourceSnapshot.