CollectionViewAdapter

The DiffableDataSource.CollectionViewAdapter serves as a NSCollectionViewDataSource that handles ListPublisher snapshots for a NSCollectionView. Subclasses of DiffableDataSource.CollectionViewAdapter may override some NSCollectionViewDataSource methods as needed. The DiffableDataSource.CollectionViewAdapter instance needs to be held on (retained) for as long as the NSCollectionView‘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

Public

  • Initializes the DiffableDataSource.CollectionViewAdapter. This instance needs to be held on (retained) for as long as the NSCollectionView‘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
        }
    )