Refactoring to a diffable data source
In iOS 13, Apple introduced the UITableViewDiffableDataSource
class. This class manages the update of a table view when the data changes and it can be used as the data source of any table view. It should be used, when possible, because implementing updates of a table view is a bit complicated and can lead to strange bugs and even crashes. In addition, the code needed to set up such a data source is often easier to read and reason about than the traditional implementation we used in the previous section.
Follow these steps to transform our implementation to one that uses a diffable data source:
- A diffable data source manages the data in the table view using a section and an item that both need to conform to the
Hashable
protocol. We already have an item we can use in the diffable data source, theToDoItem
structure. However, this structure does not yet conform toHashable
. To make it conform to that protocol, add the following code to...