ra1028 / DiffableDataSources

💾 A library for backporting UITableView/UICollectionViewDiffableDataSource.
https://ra1028.github.io/DiffableDataSources
Apache License 2.0
853 stars 69 forks source link

Add update support for diff algorithm #9

Closed Tayphoon closed 4 years ago

Tayphoon commented 5 years ago

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.

ra1028 commented 5 years ago

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

ollitapa commented 4 years ago

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)
    }
}
winstondu commented 4 years ago

@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.

https://github.com/ra1028/DiffableDataSources/pull/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.

ollitapa commented 4 years ago

@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.

ollitapa commented 4 years ago

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

winstondu commented 4 years ago

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