WWDC19
=================
Session materials: https://developer.apple.com/videos/play/wwdc2019/220/
- Traditional
UICollectionViewDataSource
- Simple & flexible
- Generating UI Updates can be challenging
- Current approach which relies on informing the UI and updating is error prone
- There is no centralized truth
performBatchUpdates()
is gone,apply()
is the new cool kid in town!- Applying a snapshot via
UICollectionViewDiffableDataSource
&UITableViewDiffableDataSource
- Call it anytime the UI requires a change
let snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.aSection])
snapshot.appendItems(items, toSection: aSection)
// delete, modify etc. is also available
======================
self.dataSource = UITableViewDiffableDataSource<Section, Item>(tableView: tableView) {
// the code we usually execute at cellForRowAtIndexPath to reflect changes
}
======================
datasource.apply(snapshot)
- Always call
apply()
, never callperformBatchUpdates()
,insertItems()
- Empty
let snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
- Current data source snapshot copy
datasource.snapshot()
insertItems(_ identifiers: [ItemIdentifierType], beforeItem beforeIdentifier: ItemIdentifierType)
moveItems
appendItems
appendSections
- Must be unique
- Conforms to
Hashable
- Data model or identifier
- e.g:
didSelectItemAt indexpath
should be converted to the datasource snapshot selection
-
Safe to call
apply()
from a bg thread- But, always call from the main queue or a bg queue, don't mix and match
-
The newly designed share sheet on iOS13 uses this new implementation