DiffableDataSource
public enum DiffableDataSource
Namespace for diffable data source types. See DiffableDataSource.TableViewAdapter
and DiffableDataSource.CollectionViewAdapter
for actual implementations
-
The
DiffableDataSource.BaseAdapter
serves as a superclass for consumers ofListPublisher
andListSnapshot
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/DiffableDataSourcesDeclaration
Swift
open class BaseAdapter<O, T> : NSObject where O : DynamicObject, T : DiffableDataSourceTarget
-
The
DiffableDataSource.CollectionViewAdapter
serves as aUICollectionViewDataSource
that handlesListPublisher
snapshots for aUICollectionView
. Subclasses ofDiffableDataSource.CollectionViewAdapter
may override someUICollectionViewDataSource
methods as needed. TheDiffableDataSource.CollectionViewAdapter
instance needs to be held on (retained) for as long as theUICollectionView
‘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/DiffableDataSourcesDeclaration
Swift
open class CollectionViewAdapter<O> : BaseAdapter<O, DefaultCollectionViewTarget<UICollectionView>>, UICollectionViewDataSource where O : DynamicObject
-
The
DiffableDataSource.TableViewAdapterAdapter
serves as aUITableViewDataSource
that handlesListPublisher
snapshots for aUITableView
. Subclasses ofDiffableDataSource.TableViewAdapter
may override someUITableViewDataSource
methods as needed. TheDiffableDataSource.TableViewAdapterAdapter
instance needs to be held on (retained) for as long as theUITableView
‘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/DiffableDataSourcesDeclaration
Swift
open class TableViewAdapter<O> : BaseAdapter<O, DefaultTableViewTarget<UITableView>>, UITableViewDataSource where O : DynamicObject
-
The
DiffableDataSource.Target
protocol allows custom views to consumeListSnapshot
diffable data similar to howDiffableDataSource.TableViewAdapter
andDiffableDataSource.CollectionViewAdapter
reloads data for their corresponding views.Declaration
Swift
public typealias Target = DiffableDataSourceTarget