# DiffableDataSource

    public enum DiffableDataSource

Namespace for diffable data source types. See `DiffableDataSource.TableViewAdapter` and `DiffableDataSource.CollectionViewAdapter` for actual implementations

### BaseAdapter

- `BaseAdapter`

  The `DiffableDataSource.BaseAdapter` serves as a superclass for consumers of `ListPublisher` and `ListSnapshot` diffable data.

      self.dataSource = DiffableDataSource.TableViewAdapter<Person>(
          tableView: self.tableView,
          dataStack: CoreStoreDefaults.dataStack,
          cellProvider: { (tableView, indexPath, person) in
              let cell = tableView.dequeueReusableCell(withIdentifier: "PersonCell") as! PersonCell
              cell.setPerson(person)
              return cell
          }
      )

  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
         )
      }

  See also

  CoreStore’s DiffableDataSource implementation is based on https://github.com/ra1028/DiffableDataSources
  See more

  #### Declaration

  Swift

      open class BaseAdapter<O, T> : NSObject where O : DynamicObject, T : DiffableDataSourceTarget

### CollectionView

- `CollectionViewAdapter`

  The `DiffableDataSource.CollectionViewAdapter` serves as a `UICollectionViewDataSource` that handles `ListPublisher` snapshots for a `UICollectionView`. Subclasses of `DiffableDataSource.CollectionViewAdapter` may override some `UICollectionViewDataSource` methods as needed. The `DiffableDataSource.CollectionViewAdapter` instance needs to be held on (retained) for as long as the `UICollectionView`‘s lifecycle.

      self.dataSource = DiffableDataSource.CollectionViewAdapter<Person>(
          collectionView: self.collectionView,
          dataStack: CoreStoreDefaults.dataStack,
          cellProvider: { (collectionView, indexPath, person) in
              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PersonCell") as! PersonCell
              cell.setPerson(person)
              return cell
          }
      )

  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
  See more

  #### Declaration

  Swift

      open class CollectionViewAdapter<O> : BaseAdapter<O, DefaultCollectionViewTarget<UICollectionView>>, UICollectionViewDataSource where O : DynamicObject

### TableViewAdapter

- `TableViewAdapter`

  The `DiffableDataSource.TableViewAdapterAdapter` serves as a `UITableViewDataSource` that handles `ListPublisher` snapshots for a `UITableView`. Subclasses of `DiffableDataSource.TableViewAdapter` may override some `UITableViewDataSource` methods as needed. The `DiffableDataSource.TableViewAdapterAdapter` instance needs to be held on (retained) for as long as the `UITableView`‘s lifecycle.

      self.dataSource = DiffableDataSource.TableViewAdapter<Person>(
          tableView: self.tableView,
          dataStack: CoreStoreDefaults.dataStack,
          cellProvider: { (tableView, indexPath, person) in
              let cell = tableView.dequeueReusableCell(withIdentifier: "PersonCell") as! PersonCell
              cell.setPerson(person)
              return cell
          }
      )

  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.TableViewAdapter` fully handles the reload animations.

  See also

  CoreStore’s DiffableDataSource implementation is based on https://github.com/ra1028/DiffableDataSources
  See more

  #### Declaration

  Swift

      open class TableViewAdapter<O> : BaseAdapter<O, DefaultTableViewTarget<UITableView>>, UITableViewDataSource where O : DynamicObject

### Target

- `Target`

  The `DiffableDataSource.Target` protocol allows custom views to consume `ListSnapshot` diffable data similar to how `DiffableDataSource.TableViewAdapter` and `DiffableDataSource.CollectionViewAdapter` reloads data for their corresponding views.

  #### Declaration

  Swift

      public typealias Target = DiffableDataSourceTarget