BaseAdapter
open class BaseAdapter<O, T> : NSObject where O : DynamicObject, T : DiffableDataSourceTarget
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-
The object type represented by this dataSource
Declaration
Swift
public typealias ObjectType = O
-
The target to be updated by this dataSource
Declaration
Swift
public let target: T
-
Initializes the
DiffableDataSource.BaseAdapter
object. This instance needs to be held on (retained) for as long as the target’s lifecycle.self.dataSource = DiffableDataSource.TableViewAdapterAdapter<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 } )
Declaration
Swift
public init(target: T, dataStack: DataStack)
Parameters
tableView
the
UITableView
to set thedataSource
of. This instance is not retained by theDiffableDataSource.TableViewAdapter
.dataStack
the
DataStack
instance that the dataSource will fetch objects fromcellProvider
a closure that configures and returns the
UITableViewCell
for the object -
Clears the target.
Declaration
Swift
open func purge(animatingDifferences: Bool = true, completion: @escaping () -> Void = {})
Parameters
animatingDifferences
if
true
, animations may be applied accordingly. Defaults totrue
. -
Reloads the target using a
ListSnapshot
. This is typically from thesnapshot
property of aListPublisher
:listPublisher.addObserver(self) { [weak self] (listPublisher) in self?.dataSource?.apply( listPublisher.snapshot, animatingDifferences: true ) } `` - parameter snapshot: the `ListSnapshot` used to reload the target with. This is typically from the `snapshot` property of a `ListPublisher`. - parameter animatingDifferences: if `true`, animations may be applied accordingly. Defaults to `true`.
Declaration
Swift
open func apply(_ snapshot: ListSnapshot<O>, animatingDifferences: Bool = true, completion: @escaping () -> Void = {})
-
Creates a new empty
ListSnapshot
suitable for building custom lists inside subclass implementations ofapply(_:animatingDifferences:completion:)
.Declaration
Swift
public func makeEmptySnapshot() -> ListSnapshot<O>
-
Returns the number of sections
Declaration
Swift
public func numberOfSections() -> Int
Parameters
indexPath
the
IndexPath
to search forReturn Value
the number of sections
-
Returns the number of items at the specified section, or
nil
if the section is not foundDeclaration
Swift
public func numberOfItems(inSection section: Int) -> Int?
Parameters
section
the section index to search for
Return Value
the number of items at the specified section, or
nil
if the section is not found -
Returns the section identifier at the specified index, or
nil
if not foundDeclaration
Swift
public func sectionID(for section: Int) -> String?
Parameters
section
the section index to search for
Return Value
the section identifier at the specified indec, or
nil
if not found -
Returns the object identifier for the item at the specified
IndexPath
, ornil
if not foundDeclaration
Swift
public func itemID(for indexPath: IndexPath) -> O.ObjectID?
Parameters
indexPath
the
IndexPath
to search forReturn Value
the object identifier for the item at the specified
IndexPath
, ornil
if not found -
Returns the
IndexPath
for the item with the specified object identifier, ornil
if not foundDeclaration
Swift
public func indexPath(for itemID: O.ObjectID) -> IndexPath?
Parameters
itemID
the object identifier to search for
Return Value
the
IndexPath
for the item with the specified object identifier, ornil
if not found -
Returns the section index title for the specified
section
if theSectionBy
for this list has provided asectionIndexTransformer
Declaration
Swift
public func sectionIndexTitle(for section: Int) -> String?
Parameters
section
the section index to search for
Return Value
the section index title for the specified
section
, ornil
if not found -
Returns the section index titles for all sections if the
SectionBy
for this list has provided asectionIndexTransformer
Declaration
Swift
public func sectionIndexTitlesForAllSections() -> [String?]