ra1028 / DiffableDataSources

💾 A library for backporting UITableView/UICollectionViewDiffableDataSource.

Home Page:https://ra1028.github.io/DiffableDataSources

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add update support for diff algorithm

Tayphoon opened this issue · comments

DifferenceKit use ContentEquatable protocol to produce items update. DataSource wrap items into struct which implement Differentiable protocol. In fact diff algorithm never produce update events, there is only one way - call reload method in DataSource. We would like to implement ContentEquatable for DataSource items and get ChangeSet with updates.

This is the same specification as Apple's official DiffableDataSource, because it uses "Hashable" instead of "Identifiable".

Just run into this issue. Could you consider introducing a wrapper struct to help out?

/// A struct whose hash value is based on
/// the differenceIdentifier of the content.
public struct Diffable<Item: Differentiable & Equatable>: Hashable {
    public var content: Item

    public init(_ content: Item) {
        self.content = content
    }
    public func hash(into hasher: inout Hasher) {
        hasher.combine(content.differenceIdentifier)
    }
}

@ollitapa , I have a PR over from my fork of this library that solves this issue. You can feel free to pull from my fork until it gets merged in.

#18

The larger issue, I think, is Apple's engineers clearly wrote their library wrong (they don't have any updates). So the question is whether we should have this library conform to Apple's mistakes, or we do it right.

@winstondu Seems good! I agree that the Apple provided API is a bit confusing, so I opted to use the Diffable wrapper I showed above. I thought it worked fine, but now that you mention the issue, with the hashValue handling, I can see that those updates are also needed.

Btw, it's curious that SwiftUI List seems to use Identifiable protocol for "differenceIdentifier" and view graph equality for the "isContentEqual"

@ollitapa , agreed. I don't understand why Apple's official DiffableDataSources uses the Hashable protocol when they have the Identifiable Protocol