DiffableDataSource
-
The
DiffableDataSource.CollectionViewAdapter
serves as aNSCollectionViewDataSource
that handlesListPublisher
snapshots for aNSCollectionView
. Subclasses ofDiffableDataSource.CollectionViewAdapter
may override someNSCollectionViewDataSource
methods as needed. TheDiffableDataSource.CollectionViewAdapter
instance needs to be held on (retained) for as long as theNSCollectionView
‘s lifecycle.self.dataSource = DiffableDataSource.CollectionViewAdapter<Person>( collectionView: self.collectionView, dataStack: CoreStoreDefaults.dataStack, itemProvider: { (collectionView, indexPath, person) in let item = collectionView.makeItem(withIdentifier: .collectionViewItem, for: indexPath) as! PersonItem item.setPerson(person) return item } )
The dataSource can then apply changes from a
ListPublisher
as shown:listPublisher.addObserver(self) { [weak self] (listPublisher) in self?.dataSource?.apply( listPublisher.snapshot, animatingDifferences: true ) }
DiffableDataSource.CollectionViewAdapter
fully handles the reload animations.See also
CoreStore’s DiffableDataSource implementation is based on https://github.com/ra1028/DiffableDataSources