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 nil before 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

    collectionView

    The collection view managed by the data source.

    onCellReconfiguration

    An optional closure called after cellProvider returns 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.

    cellProvider

    A 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

    snapshot

    The snapshot that replaces the current state.

    commitAlongsideUpdates

    A closure that commits application-owned models before the collection view reloads its data.

    completion

    An 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
    ) async

    Parameters

    snapshot

    The snapshot that replaces the current state.

    commitAlongsideUpdates

    A 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

    snapshot

    The snapshot that replaces the current state.

    animatingDifferences

    A Boolean value that determines whether changes are animated.

    commitAlongsideUpdates

    A closure that commits application-owned models within the collection-view update transaction.

    completion

    An 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
    ) async

    Parameters

    snapshot

    The snapshot that replaces the current state.

    animatingDifferences

    A Boolean value that determines whether changes are animated.

    commitAlongsideUpdates

    A 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

    id

    The item identifier to locate.

    Return Value

    The item’s index path, or nil when the identifier is nil or 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

    indexPath

    The index path to locate.

    Return Value

    The item identifier, or nil when 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) -> Int

    Parameters

    collectionView

    The 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) -> Int

    Parameters

    collectionView

    The collection view requesting the information.

    section

    The index of the section.

    Return Value

    The number of items in the section, or 0 when 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) -> UICollectionViewCell

    Parameters

    collectionView

    The collection view requesting the cell.

    indexPath

    The index path of the requested cell.

    Return Value

    The cell returned by the cell provider.