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.BaseAdapterobject. 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
tableViewthe
UITableViewto set thedataSourceof. This instance is not retained by theDiffableDataSource.TableViewAdapter.dataStackthe
DataStackinstance that the dataSource will fetch objects fromcellProvidera closure that configures and returns the
UITableViewCellfor the object -
Clears the target.
Declaration
Swift
open func purge(animatingDifferences: Bool = true, completion: @escaping () -> Void = {})Parameters
animatingDifferencesif
true, animations may be applied accordingly. Defaults totrue. -
Reloads the target using a
ListSnapshot. This is typically from thesnapshotproperty 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
ListSnapshotsuitable 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() -> IntParameters
indexPaththe
IndexPathto search forReturn Value
the number of sections
-
Returns the number of items at the specified section, or
nilif the section is not foundDeclaration
Swift
public func numberOfItems(inSection section: Int) -> Int?Parameters
sectionthe section index to search for
Return Value
the number of items at the specified section, or
nilif the section is not found -
Returns the section identifier at the specified index, or
nilif not foundDeclaration
Swift
public func sectionID(for section: Int) -> String?Parameters
sectionthe section index to search for
Return Value
the section identifier at the specified indec, or
nilif not found -
Returns the object identifier for the item at the specified
IndexPath, ornilif not foundDeclaration
Swift
public func itemID(for indexPath: IndexPath) -> O.ObjectID?Parameters
indexPaththe
IndexPathto search forReturn Value
the object identifier for the item at the specified
IndexPath, ornilif not found -
Returns the
IndexPathfor the item with the specified object identifier, ornilif not foundDeclaration
Swift
public func indexPath(for itemID: O.ObjectID) -> IndexPath?Parameters
itemIDthe object identifier to search for
Return Value
the
IndexPathfor the item with the specified object identifier, ornilif not found -
Returns the section index title for the specified
sectionif theSectionByfor this list has provided asectionIndexTransformerDeclaration
Swift
public func sectionIndexTitle(for section: Int) -> String?Parameters
sectionthe section index to search for
Return Value
the section index title for the specified
section, ornilif not found -
Returns the section index titles for all sections if the
SectionByfor this list has provided asectionIndexTransformerDeclaration
Swift
public func sectionIndexTitlesForAllSections() -> [String?]
View on GitHub
BaseAdapter Class Reference