DifferenceKit provides a default implementation of Differentiable for Hashable. However, when using the default implementation, DifferenceKit always trigers a delete -> insert change instead of a update change.
Unexpected
I have a simple model that uses the default differenceIdentifier implementation.
struct ElementModel {
// The identifier.
var id: Int
// The contents.
var title: String
var value: String
}
// Use the default `differenceIdentifier` implementation.
extension ElementModel: Hashable, Differentiable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
Create a changeset between two items that have the same id but different value.
let list1 = [ElementModel(id: 1, title: "color", value: "red")]
let list2 = [ElementModel(id: 1, title: "color", value: "blue")]
let changeset = StagedChangeset(source: list1, target: list2)
changeset.forEach { print($0) }
Unfortunately, the output message shows that there is a delete -> insert update.
Changeset(
data: [],
elementDeleted: [
[element: 0, section: 0]
]
)
Changeset(
data: [
ElementModel(id: 1, title: "color", value: "blue")
],
elementInserted: [
[element: 0, section: 0]
]
)
Expected
If I explicitly implement the differenceIdentifier property by return hashValue, everything will be fine.
extension ElementModel: Hashable, Differentiable {
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
var differenceIdentifier: Int {
// Return `hashValue` insteal of `Self`
return hashValue
}
}
The expected update change.
Changeset(
data: [
ElementModel(id: 1, title: "color", value: "blue")
],
elementUpdated: [
[element: 0, section: 0]
]
)
Question
I'm not sure if this is a mistake of my code of a bug of DifferenceKit.
DifferenceKit provides a default implementation of Differentiable for Hashable, but the implementation of differenceIdentifier just return the Hashable instance itself. I think maybe the hashValue is the right return value.
// Return self
var differenceIdentifier: Self {
return self
}
// Return hashValue
var differenceIdentifier: Int {
return hashValue
}
DifferenceKit provides a default implementation of
DifferentiableforHashable. However, when using the default implementation, DifferenceKit always trigers adelete -> insertchange instead of aupdatechange.Unexpected
I have a simple model that uses the default
differenceIdentifierimplementation.Create a changeset between two items that have the same
idbut differentvalue.Unfortunately, the output message shows that there is a
delete -> insertupdate.Changeset( data: [], elementDeleted: [ [element: 0, section: 0] ] ) Changeset( data: [ ElementModel(id: 1, title: "color", value: "blue") ], elementInserted: [ [element: 0, section: 0] ] )Expected
If I explicitly implement the
differenceIdentifierproperty by returnhashValue, everything will be fine.The expected
updatechange.Changeset( data: [ ElementModel(id: 1, title: "color", value: "blue") ], elementUpdated: [ [element: 0, section: 0] ] )Question
I'm not sure if this is a mistake of my code of a bug of DifferenceKit.
DifferenceKit provides a default implementation of
DifferentiableforHashable, but the implementation ofdifferenceIdentifierjust return theHashableinstance itself. I think maybe thehashValueis the right return value.