Diffable Data Sources
Diffable Data Sources
HOW TO
USE DIFFABLE DATA
SOURCES IN SWIFT
Shubham Singh
@shubham_iosdev
IOS TUTORIALS
Let us first look into the working of Diffable Data source for a
TableView.
Note- You can use Diffable Data Sources in projects running >= iOS 13
Shubham Singh
@shubham_iosdev
IOS TUTORIALS
1. UITableViewDiffableDataSource
This class has two generic types, one for Section and the other
one for Item(Row). The only requirement is that both of them
should conform to Hashable protocol.
The data source will use hash values to determine changes in the
data set. So it’s important they both the Section and Item are
unique.
Shubham Singh
@shubham_iosdev
IOS TUTORIALS
For the third parameter, you have to pass the tableView for which
the cells needs to be returned and displayed.
Shubham Singh
@shubham_iosdev
IOS TUTORIALS
2. NSDiffableDataSourceSnapshot
You can see that it has the same Section and Item types
requirement as the DiffableDataSource.
With a snapshot, you can set the initial state of the data for The
table/Collection views and later, Update the data when needed.
The data in a snapshot is made up of the sections and items you want
to display. This data is reflected in the tableView when passed to the
dataSource.
Shubham Singh
@shubham_iosdev
IOS TUTORIALS
Shubham Singh
@shubham_iosdev
IOS TUTORIALS
Tutorial
Let us create an app to display books based on trends. First, let us
create the model for the same.
Shubham Singh
@shubham_iosdev
IOS TUTORIALS
PS- You still need to set the tableView delegate and implement rowHeight, section etc.
Shubham Singh
@shubham_iosdev
IOS TUTORIALS
Once a new snapshot is passed the TableView finds the minimum amount
of changes needed to go from old to new and animates the changes.
Shubham Singh
@shubham_iosdev
IOS TUTORIALS
Shubham Singh
@shubham_iosdev
COMMENT
LIKE