개요

스크린샷 2022-07-20 오후 5.12.09.png

ezgif-5-6e9e954789.gif

TableViw 혹은 CollectionView를 그리기 위한 데이터를 관리하고 UI를 업데이트

( 해당 글에선 tableView로 설명 )

기존 DataSource와 달리 달라진 부분을 추적하여 자연스럽게 UI를 업데이트 (애니메이션 효과)

왜 필요함?

기존 dataSoruce는 데이터가 업데이트되면 **tableView.reloadData()**로 동기화를 해 주었다

하지만 위 메서드는 tableView를 한번에 업데이트 하므로 애니메이션 효과가 적용이 안되 사용자 경험에 나쁘다

하지만 DiffableDataSource같은 경우는 변경된 데이터가 자연스러운 애니메이션 효과로 적용되는것을 볼 수 있다

DiffableDataSource를 사용함으로서 얻게되는 이점

어떻게 사용함?

  1. DiffableDataSource를 tableView에 연결
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
		}
}
  1. dataSource에 snapshot 적용
private func applySnapshot() {
		var snapshot = NSDiffableDataSourceSnapshot<TableViewSection, Item>()
    snapshot.appendSections([.main])
    snapshot.appendItems(tableViewItem)
    self.dataSource?.apply(snapshot, animatingDifferences: true)
}

왜 Hashable 해야함?