This article is a follow-up to the previous one.

Currently, when we refresh a table view, all the cells get reloaded. This is acceptable in the present scenario as we anticipate receiving new data (a random number of jokes each time). However, let’s assume we have a feed of posts that infrequently updates, and reloading the entire table view each time results in unnecessary work. Another example would be when implementing pagination, where we add new items and then reload the entire table view, rather than just displaying the new items. We could achieve this without reloading all cells by using reloadRows(at: with:) or reloadItems(at:), but it would require additional calculations to provide accurate indexPaths. In such cases, using UITableViewDiffableDataSource can help us resolve these issues by managing all the complex operations and updating only the necessary changes in an efficient manner.
The concept is to substitute the previous data source methods with UITableViewDiffableDataSource. Additionally, we had been manually refreshing the table view using the didSet property observation.
This is the object you use to manage data and provide cells for a table view.
A diffable data source object is a specialized type of data source that works together with your table view object. It provides the behavior you need to manage updates to your table view’s data and UI in a simple, efficient way. It also conforms to the UITableViewDataSource protocol and provides implementations for all of the protocol’s methods.
To connect a diffable data source to a table view, you create the diffable data source using its init(tableView:cellProvider:)initializer, passing in the table view you want to associate with that data source. You also pass in a cell provider, where you configure each of your cells to determine how to display your data in the UI.
1. Create diffable data source and implement a cell provider to configure your table view’s cells.
Within the angle brackets, we must indicate the section type as Int, and the model type as Joke, which must conform to the Hashable protocol.
2. Generate the current state of the data.
Basically this is the code that we had in tableView(_:cellForRowAt:)
3. Display the data in the UI.
We generate the current state of the data and display the data in the UI by constructing and applying a snapshot. Also in updateJokes we updating the snapshot with new items. This way we can get rid of jokes property array that we set before.
4. Don’f forget to connect a diffable data source to your table view.
tableView.dataSource = dataSource
Apple changed the behavior of the method apply(_:animatingDifferences:) of the UITableViewDiffableDataSource and UICollectionViewDiffableDataSource classes.
On iOS 14 and before, calling apply(snapshot, animatingDifferences: false) was equivalent to calling reloadData. And calling apply(snapshot, animatingDifferences: true) would perform a diff on the data source and only update cells that the data changed.
As of iOS 15, applying a snapshot using this API will always perform a diff, optionally animate the UI updates based on the value of animatingDifferences.
To explicitly reload the whole table/collection without diffing, you can now call applySnapshotUsingReloadData(_:completion:) instead.
Please bear in mind the distinction and select the methods that are appropriate for your requirements.
The project is available on GitHub.
Thank you for taking the time to read this article. I hope it has been beneficial for you. Feel free to leave a comment or ask any questions you may have.
<hr><p>Mastering Table/Collection View DiffableDataSource: A Comprehensive Guide was originally published in ITNEXT on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>