How to properly implement single update for a single cell with identifier?

Consider, that you have a struct that represents a Row.

Code Block
struct Row {
var diffable: AnyHashable
var yourDataProviderForCell: Provider
}
struct ListViewModel {
var providers: [Provider] { didSet { self.rows = rowsFromProviders(self.providers) } }
@Published var rows: [Row]
}


So, it is simple, you have a list of providers that store all required information for cell.

You encapsulate everything in ListViewModel, so, you could add this ListViewModel to your ViewController, right?

Code Block
class ListViewController {
var viewModel: ListViewModel
func configured() {
self.viewModel.$rows {
// Apply snapshot.
}
}
}


Everything will ok fine when you add required protocols to Row:

Code Block
extension Row: Hashable {
func == (lhs: Self, rhs: Self) {
lhs.diffable == rhs.diffable
}
func hash(inout hasher: Hasher) {
hasher.combine(self.diffable)
}
}


Look at what we have now.
  1. -

  2. ListViewController subscribed on @Published rows

  3. ListViewModel updates var providers and in didSet we update rows.

  4. Row has var diffable: AnyHashable which encapsulates unique nature of Row. ( So, it should be unique, but it contains various information from Provider )

And it will work fine after we add following initializer.

Code Block
extension Row {
init(provider: Provider) {
self.provider = provider
self.diffable = Provider.DiffableBuilder(provider)
}
}


We add Provider.DiffableBuilder which will build AnyHashable key for Row that will be used in Equatable and Hashable for Row and will determine if cell is changed or not.

The technique above describes one possible solution to update cells.

We simply store a snapshot or a piece of cell data that will be determined as unique set.

In this circumstances we could do nothing and just apply updates via append items without reload cells.

Is it a good technique and should it be banned or not?
How to properly implement single update for a single cell with identifier?
 
 
Q