TableViw 혹은 CollectionView를 그리기 위한 데이터를 관리하고 UI를 업데이트
( 해당 글에선 tableView로 설명 )
기존 DataSource와 달리 달라진 부분을 추적하여 자연스럽게 UI를 업데이트 (애니메이션 효과)
기존 dataSoruce는 데이터가 업데이트되면 **tableView.reloadData()**로 동기화를 해 주었다
하지만 위 메서드는 tableView를 한번에 업데이트 하므로 애니메이션 효과가 적용이 안되 사용자 경험에 나쁘다
하지만 DiffableDataSource같은 경우는 변경된 데이터가 자연스러운 애니메이션 효과로 적용되는것을 볼 수 있다
private func configureDiffableDataSource() {
dataSource = UITableViewDiffableDataSource<TableViewSection, Item>(tableView: tableView) { table, indexPath, item -> UITableViewCell? in
guard let cell = self.tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? Cell else { return UITableViewCell() }
cell.label.text = item.content
return cell
}
}
private func applySnapshot() {
var snapshot = NSDiffableDataSourceSnapshot<TableViewSection, Item>()
snapshot.appendSections([.main])
snapshot.appendItems(tableViewItem)
self.dataSource?.apply(snapshot, animatingDifferences: true)
}