Consider, that you have a struct that represents a 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?
Everything will ok fine when you add required protocols to Row:
Look at what we have now.
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?
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.
-
ListViewController subscribed on @Published rows
ListViewModel updates var providers and in didSet we update rows.
Row has var diffable: AnyHashable which encapsulates unique nature of Row. ( So, it should be unique, but it contains various information from Provider )
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?