I have been playing around with the new iOS 13 diffable data sources and find them very easy and straight forward to use.
However I am unsure about 1 thing. Is it recommended to use the snapshot property of the diffable data sources for example in UITableViewDelegate e.g to get section height?
This was my original approach
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
let section = dataSource.snapshot().sectionIdentifiers[section]
return section.footerHeight
}
dataSource
being my diffable data source property, and footerHeight
is a computed property in my Section enum used by the diffable data source.
I have seen some tutorials that create a reference to the latest snapshot
var currentSnapshot: NSDiffableDataSourceSnapshot?
and update it every time they apply a new snapshot the table data source. So my original code could now be something like this
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
let section = currentSnapshot?.sectionIdentifiers[section]
return section?.footerHeight ?? 0
}
Judging from the Apple documentation the snapshot()
method of the data source gets created every time, so I feel like my 1st approach is not very efficient as for each section, or potentially even row, a new snapshot copy gets created.
Is approach 2 the better way? Or is creating a snapshot very efficient and thus approach 1 is fine?
In practice both ways are identical. A snapshot is created once respectively after calling
apply