ChatLayoutDiffableDataSource
@MainActor
open class ChatLayoutDiffableDataSource<SectionID, ItemID> : NSObject, UICollectionViewDataSource where SectionID : Hashable, SectionID : Sendable, ItemID : Hashable, ItemID : Sendable
A diffable data source created specifically to coordinate UICollectionView.reconfigureItems(at:) with
CollectionViewChatLayout.reconfigureItems(at:).
Apple’s UICollectionViewDiffableDataSource forwards snapshot application to private __UIDiffableDataSource and
_UIDiffableDataSourceViewUpdater objects. Those objects use the private _performDiffableUpdate(_:) transaction and
an internal commitAlongsideHandler to replace identifier state while UIKit applies the view updates.
ChatLayoutDiffableDataSource cannot use those private hooks, so commitAlongsideUpdates exposes the equivalent
synchronization point within its public performBatchUpdates transaction. Clients must update any application-owned
models read by the cell provider and layout delegate in that closure so they remain consistent with the new snapshot.
-
The snapshot currently represented by the collection view, or
nilbefore the first snapshot is applied.Declaration
Swift
@MainActor open var snapshot: NSDiffableDataSourceSnapshot<SectionID, ItemID>? { get } -
Constructor.
Declaration
Swift
@MainActor public init( collectionView: UICollectionView, onCellReconfiguration: ((UICollectionViewCell) -> Void)? = nil, cellProvider: @escaping (UICollectionView, IndexPath, ItemID) -> UICollectionViewCell? )Parameters
collectionViewThe collection view managed by the data source.
onCellReconfigurationAn optional closure called after
cellProviderreturns a cell for a reconfigured item. The closure runs within the collection view’s batch-update transaction and, when differences are animated, within that update animation. Use it to perform additional actions alongside the update or to animate changes within the cell itself.cellProviderA closure that creates and configures a cell for an item identifier.
-
Replaces the current snapshot and reloads the collection view.
Declaration
Swift
@MainActor open func applySnapshotUsingReloadData( _ snapshot: NSDiffableDataSourceSnapshot<SectionID, ItemID>, commitAlongsideUpdates: () -> Void, completion: (() -> Void)? = nil )Parameters
snapshotThe snapshot that replaces the current state.
commitAlongsideUpdatesA closure that commits application-owned models before the collection view reloads its data.
completionAn optional closure called after the new snapshot is applied.
-
Replaces the current snapshot, reloads the collection view, and returns after the new snapshot is applied.
Declaration
Swift
@MainActor open func applySnapshotUsingReloadData( _ snapshot: NSDiffableDataSourceSnapshot<SectionID, ItemID>, commitAlongsideUpdates: () -> Void ) asyncParameters
snapshotThe snapshot that replaces the current state.
commitAlongsideUpdatesA closure that commits application-owned models before the collection view reloads its data.
-
Applies a snapshot while committing application-owned models alongside the collection-view update.
Declaration
Swift
@MainActor open func apply( _ snapshot: NSDiffableDataSourceSnapshot<SectionID, ItemID>, animatingDifferences: Bool, commitAlongsideUpdates: @escaping () -> Void, completion: (() -> Void)? = nil )Parameters
snapshotThe snapshot that replaces the current state.
animatingDifferencesA Boolean value that determines whether changes are animated.
commitAlongsideUpdatesA closure that commits application-owned models within the collection-view update transaction.
completionAn optional closure called after the collection view finishes applying the update.
-
Applies a snapshot and returns after the collection view finishes applying the update.
Declaration
Swift
@MainActor open func apply( _ snapshot: NSDiffableDataSourceSnapshot<SectionID, ItemID>, animatingDifferences: Bool, commitAlongsideUpdates: @escaping () -> Void ) asyncParameters
snapshotThe snapshot that replaces the current state.
animatingDifferencesA Boolean value that determines whether changes are animated.
commitAlongsideUpdatesA closure that commits application-owned models within the collection-view update transaction.
-
Returns the current index path for an item identifier.
Declaration
Swift
@MainActor open func indexPath(for id: ItemID?) -> IndexPath?Parameters
idThe item identifier to locate.
Return Value
The item’s index path, or
nilwhen the identifier isnilor is not present in the current snapshot. -
Returns the item identifier at an index path in the current snapshot.
Declaration
Swift
@MainActor open func itemIdentifier(for indexPath: IndexPath) -> ItemID?Parameters
indexPathThe index path to locate.
Return Value
The item identifier, or
nilwhen the index path is not present in the current snapshot. -
Returns the number of sections represented by the current snapshot.
Declaration
Swift
@MainActor open func numberOfSections(in collectionView: UICollectionView) -> IntParameters
collectionViewThe collection view requesting the information.
Return Value
The number of sections in the current snapshot.
-
Returns the number of items in a section represented by the current snapshot.
Declaration
Swift
@MainActor open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> IntParameters
collectionViewThe collection view requesting the information.
sectionThe index of the section.
Return Value
The number of items in the section, or
0when the section does not exist. -
Returns the cell provided for the item identifier at an index path.
Declaration
Swift
@MainActor open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCellParameters
collectionViewThe collection view requesting the cell.
indexPathThe index path of the requested cell.
Return Value
The cell returned by the cell provider.