[WWDC 2019] Advances in UI Data Sources
https://developer.apple.com/videos/play/wwdc2021/10252/
Make blazing fast lists and collection views - WWDC21 - Videos - Apple Developer
Build consistently smooth scrolling list and collection views: Explore the lifecycle of a cell and learn how to apply that knowledge to...
developer.apple.com
๋ฅผ ์ฝ๊ธฐ ์ ์ DiffableDataSource์ ๋ํด ์ดํดํ๊ณ ์ Advances in UI Data Sources๋ฅผ ๋จผ์ ๋ณด๊ฒ ๋์์ต๋๋ค. ๊ณต๋ถํด๋ณด๊ณ ์ถ์๋ ๋ด์ฉ์ด๋ผ ์ญ ํ๋ฆ์ ์ ๋ฆฌํด๋ณด๋ ค๊ณ ํฉ๋๋ค. ์ฝ๊ณ 2021๋ ๋์ ๋ฐํ๋ ์ฑ๋ฅ ๊ฐ์ ์ธ์ ๋ ๋นจ๋ฆฌ ๋ณด๊ณ ์ถ๊ตฐ์ ใ ใ
UICollectionviewDatasource protocol์ ์ค์ํ ๋ ์๋์ ๊ฐ์ด 3๊ฐ์ ํจ์๋ฅผ ๊ตฌํํด์ผ ํฉ๋๋ค.
์ด๊ฒ์ ์์ฑ์ด ๊ฐ๋จํ๊ณ ์ ์ฐํ๊ณ ์ต์ํ ๋ฐฉ์์ธ๋ฐ์.
๊ทธ์ ์ UI์ Controller๊ฐ ์๋ก ์ํตํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๋ด๋ด ์๋ค.
์ฐ์ , UI layer๊ฐ numberOfItemsInSection์ ํตํด ๋ ๋๋งํ item section๊ณผ cell ๋ฑ์ ๋ฌ๋ผ๊ณ ํฉ๋๋ค.
web service๋ก๋ถํฐ response๋ฅผ ๋ฐ์์ค๊ณ ์ด๋ฅผ UI์๊ฒ ์๋ฆฝ๋๋ค. ๊ทธ๋ฌ๋ฉด UI๋ ๋ฐ์๋ค์ธ ๋ณ๊ฒฝ ์ฌํญ์ ์ ๋ฐ์ดํธ ํ ์ง ๋ง์ง ๊ฒฐ์ ํ๊ฒ ๋์ฃ .
ํ์ง๋ง ์ ๋ฐ์ดํธ๊ฐ ์๋ชป๋๋ค๋ฉด !
โ์ด๋ฐ ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉฐ ์ข์ ํ๊ณค, reloadData()๋ฅผ ์ฌ์ฉํ๊ฒ ๋ฉ๋๋ค.
ํ์ง๋ง realodData()์ ์ ๋๋ฉ์ด์ ํจ๊ณผ๊ฐ ์์ ๊ฒ์ด๋ฉฐ, ์ฌ์ฉ์ ๊ฒฝํ์ ์ ํ์ํฌ ๊ฒ์ ๋๋ค.
Data source๋ก ํ๋ํ๋ controller๋ truth์ ๊ณ์ํด์ ๋ณํํ๋ ์์ ์ ๊ณ ์ ํ ๋ฒ์ ์ ๊ฐ์ง๊ณ ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ UI๋ truth์ ์์ ์ ๊ณ ์ ํ ๋ฒ์ ์ ๊ฐ์ง๊ณ ์์ต๋๋ค. UILayer ์ฝ๋๋ ํญ์ sync ์ํ๋ฅผ ์ ์ง ํ๋๋ก ํ๋ ์ฑ
์์ ๊ฐ์ง๊ณ ์์ต๋๋ค. ํ์ฌ๋ Centralized truth๋ ๊ฐ๋
์ด ์๊ธฐ ๋๋ฌธ์, sync๊ฐ ๋ง์ง ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๊ฒ ๋ฉ๋๋ค.
๊ทธ๋์ ์ ์ ๊ทผ ๋ฐฉ์์ธ Diffable Datasource๋ฅผ ์๊ฐํ๊ณ ์ ํฉ๋๋ค.
performBatchUpdates()์ ๊ฐ์ ๋ฉ์๋๊ฐ ์์ผ๋ฉฐ apply()๋ผ๋ ๋จ์ผ ๋ฉ์๋๊ฐ ์์ต๋๋ค. apply()๋ ๊ฐ๋จํ๊ณ ์๋์ผ๋ก diffingํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ snapshot์ด๋ผ๋ ๊ฐ๋
์ ๋์
ํ๋๋ฐ์
์ด๊ฒ์ UI state์ truth์
๋๋ค. ๊ทธ๋ฆฌ๊ณ indexpath๋์ , uniqueํ identifier๋ฅผ ์ฌ์ฉํฉ๋๋ค.
BAR, FOO, BIF๋ indentifier๋ก ํ์ฌ ์ํ์์ apply() ํ์ฌ ์ ์ค๋ ์ท์ผ๋ก ์ ๋ฐ์ดํธ ๋ฉ๋๋ค. ์ด๋ก ์ธํด apply()๋ ํ์ฌ state์ ์ state๋ฅผ ์๊ฒ ๋๊ณ , ๊ทธ๊ฒ์ UI์ ์ ๋ฐ์ดํธ ์ํต๋๋ค.
์ด๋ฅผ ์ํ ํด๋์ค๋ 4๊ฐ์ง๊ฐ ์์ต๋๋ค.
demo๋ฅผ ๋ณด๋ฉฐ ์ด๋ฅผ ์ฌ์ฉํด๋ด ์๋ค. searchBar์ ์ฐ ์ด๋ฆ์ ๊ฒ์ํ๋ฉด ์๋ collectionView์์ ์๋์ผ๋ก filter๋๋ ์ํฉ์ ๊ตฌํํด๋ณผ ๊ฒ์ ๋๋ค.
๋จผ์ searchBar์ text๋ฅผ ์
๋ ฅํ๋ ๊ฒ์ ๊ฐ์งํ๋ textDidChange ํจ์์
๋๋ค. text๊ฐ ์
๋ ฅ๋๋ฉด performQuery(with: searhText)๊ฐ ์คํ๋๊ฒ ์ฃ
performQuery() ๋ฉ์๋๋ฅผ ๋ ์์ธํ ๋ด
์๋ค.
์ฐ์ mountainsController.filteredMountains(with: filter)๋ฅผ ํตํด filtering๋ model data ์ด๋ฆ ์์ผ๋ก ์ ๋ ฌํด ๊ฐ์ ธ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ 3๊ฐ์ง ์คํ
์ ๊ฑฐ์นฉ๋๋ค.
1. NSDiffableDataSourceSnapshot์ ์์ฑ ํฉ๋๋ค.
2. Snapshot์ ์ด๊ธฐ์ empty์
๋๋ค. ๋ฐ๋ผ์ ์ํ๋ ์น์
๊ณผ ํญ๋ชฉ์ผ๋ก ์ฑ์ฐ๋ ๊ฒ์ ์ฐ๋ฆฌ์๊ฒ ๋ฌ๋ ค ์์ต๋๋ค. ํ์ฌ ๋ฐ๋ชจ์์ ์ฐ๋ฆฌ๋ ๋ณด์ฌ์ค section์ด ํ๋์ด๋ฏ๋ก item์ ๋จ์ผ section์ ๋ฃ์ด์ค๋๋ค.
3. ๋ค์์ผ๋ก ์ด ์
๋ฐ์ดํธ์์ ํ์ํ๋ ค๋ item์ idenfifier๋ฅผ ์ถ๊ฐํฉ๋๋ค.
4. ๊ทธ๋ฆฌ๊ณ datasource์๊ฒ ์ด snapshot์ apply()ํ๋ ๋ฉ์๋๋ฅผ ํธ์ถํฉ๋๋ค.
NSDiffableDataSourceSnapshot์ Generic Class์ ๋๋ค. ๊ทธ๋ฌ๋ฏ๋ก, ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ๊ธฐ๋ก ๊ฒฐ์ ํ Section IdentifierType ๋ฐ Item IdentifierType์ ์ํด ๋งค๊ฐ๋ณ์ํ๋ฉ๋๋ค.
๋จผ์ , SectionIdentifierType์ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ์ด๊ฒ์ ํ๋์ ์น์
๋ง ์๋ ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ์ ์ ๋ง ํธ๋ฆฌํ ๊ธฐ์ ์
๋๋ค. section์ ์ด๊ฑฐํ์ผ๋ก section case๋ค์ ๋ช
์ํด์ฃผ๊ธฐ๋ง ํ๋ฉด ๋ฉ๋๋ค.
Item IdentifierType์ธ mountains์ model data์
๋๋ค. Mountain struct๋ hasable๋ก ๋ฐ๋ก identifier๋ฅผ ๋ช
์ํ์ง ์์๋ ๋๊ฒ ํฉ๋๋ค. ๊ฐ๊ฐ์ mountains๋ ๊ฐ๊ฐ์ uniqueํ identifier๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ datasource๋ฅผ ์ธ์ํ ์ ์๋๋ก configureDataSource() ๋ฉ์๋๋ฅผ ๊ตฌํํด ๋์์ต๋๋ค.
์ ๊นํ์ ์์ ์ฝ๋๋ฅผ ์ฌ๋ ค ๋์์ต๋๋ค.
https://github.com/spqjf12345/DiffableDataSource
๋ ๋ค๋ฅธ ์์๋ฅผ ๋ด
์๋ค. wifiSettingViewController์
๋๋ค.
์ด๋ฒ์๋ section์ด ๋๊ฐ ์
๋๋ค. ์ฒซ๋ฒ์งธ config ์น์
์ ์์ดํ์ด๋ฅผ ๋๊ณ ์ผค ์ ์์ผ๋ฉฐ, ๊ทธ ๋ฐ์๋ ์ฐ๊ฒฐ ํ ์ ์๋ ์์ดํ์ด๋ฅผ ํ์งํด ๋์ ์ผ๋ก ๋ณด์ฌ์ฃผ๋ ์น์
์ด ์์ต๋๋ค.
๋ณ๊ฒฝ ์ฌํญ์ด ์์๋๋ง๋ค updateUI() ๋ฉ์๋๋ฅผ ํธ์ถํด ๋ณํ๋ฅผ ๊ฐ์งํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ฌ์ฉ์๊ฐ ์์ดํ์ด ๋ฒํผ์ ํ ํด์์๋ ์ด ๋ฉ์๋๊ฐ ํธ์ถ๋ฉ๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก snapshot์ ์์ฑํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ฒซ๋ฒ์งธ ์น์
์ธ config๋ฅผ ์ถ๊ฐํ๊ณ configItems ์์ดํ
์ config ์น์
์ ์ถ๊ฐํฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ wifiEnabled ์ํ๊ฐ์ด true๋ผ๋ฉด networks ์น์
์ networkItems์ ๋ฃ์ด์ค๋๋ค.
๋ฃ์ด์ค snapshot์ apply() ํจ์๋ฅผ ํธ์ถํด datasource๋ฅผ ์
๋ฐ์ดํธ ํด์ค๋๋ค.
์๊น์ ๋ฌ๋ฆฌ UITableViewDiffalbeDataSource์ ์์ฑํ๋ ๋ฐฉ๋ฒ์
๋๋ค. ์ธ๋ป ๋ณด๊ธฐ์๋ ๋ ๋ณต์กํด ๋ณด์ด์ง๋ง ์ค์ ๋ก๋ ๋ค์ํ ์ ํ์ ํญ๋ชฉ์ด ์๊ธฐ ๋๋ฌธ์ ๊ทธ๋ ๊ฒ ๋ณด์ผ ๋ฟ์
๋๋ค.
์ฐ๋ฆฌ์ ๋ง์ง๋ง ์๋ ์๋ง๋ ๊ฐ์ฅ ์ฌ๋ฏธ์๊ฒ ๋ณผ ์ ์์ ๊ฒ์
๋๋ค. ์ฌ๊ธฐ์ ์์ ๊ฒฌ๋ณธ์ผ๋ก ํ์๋๋ ํญ๋ชฉ์ ํ์ํ๋ UICollectionView๊ฐ ๋ค์ ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ๋ค์ ์ฒ์์ ์์๋ณ๋ก ๋ฌด์์ ์์๋ก ๋์ด ์์ต๋๋ค. ์ ๋ ฌ ๋ฒํผ์ ํญํ๋ฉด ์คํํธ๋ผ ์์๋ก ๋ฐ๋ณต์ ์ผ๋ก ์ ๋ ฌ๋๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค.
์ด ์์ ๋ ์
๋ฐ์ดํธ๋ฅผ ๊ตฌ์ฑ(construct)ํ๊ณ ์ปค๋ฐ(commit)ํ๋ ๋ฐฉ์์ด ์ ์์ ์ ์ฝ๊ฐ ๋ค๋ฆ
๋๋ค. ์ ๋ ฌ ๊ณผ์ ์ ๊ฐ ๋จ๊ณ๋ง๋ค ๋ณผ ์ ์๋ ๋ฉ์๋๊ฐ ์๋๋ฐ ์ด ๋ฉ์๋์์ ์ฐ์์ ์ธ ์๋ก์ด ์ํ๋ฅผ ์ ๊ณตํ๊ณ ์ด๋๋ง๋ค snapshopt์ ์์ฑํ๊ณ apply() ํฉ๋๋ค.
performSortStep() ๋ฉ์๋๋ฅผ ๋ด ๋ด
์๋ค. ๋ฐ๋ณตํด์ ์๊ธฐํ๋ฏ์ด 1. Snapshot์ ์ป๊ณ 2. ๊ทธ๊ฒ์ populateํ๊ณ 3. Apply ํ ๊ฒ์
๋๋ค. ํ์ง๋ง ์ด๋ฒ ์์ ์์ ์๋ก์ด, ๋น์ด์๋ snapshot์ ์์ฑํ๋ ๊ฒ์ด ์๋ DiffableDataSource์๊ฒ ํ์ฌ์ snapshot์ ์์ฒญํ ๊ฒ์
๋๋ค.
updateSnapshot์ ํ์ฌ datasource๋ฅผ ๋ณด์ฌ์ฃผ๋ truth๋ก ์ฑ์์ ธ ์์ ๊ฒ์
๋๋ค.
์์ ์ ๋ดค๋ appendItems() ๋ฉ์๋๊ฐ ์๊ณ ์ถ๊ฐ๋ก deleteItems๋ฅผ ๋ณผ ์ ์์ต๋๋ค. ๊ทธ ์ธ์๋ ๋ค์ํ ๋ฉ์๋๋ค์ด ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ dataSource์ updateSnapshot์ apply ํฉ๋๋ค.
๊ทธ๋ฆฌ๊ณค ์
๋ฐ์ดํธ๋ datasource์ ๋ํด update UI ํด์ฃผ๋ ์ฝ๋๋ ์๋์ ๊ฐ์ต๋๋ค.
Consideration
reloadData(), intertItems()๊ณผ ๊ฐ์ ๋ฉ์๋๋ฅผ ํธ์ถํ์ง ์๊ณ ์ค์ง apply()๋ง์ผ๋ก ์
๋ฐ์ดํธ ํฉ๋๋ค.
sanpshot์ ์์ฑํ๋ ๋๊ฐ์ง ๋ฐฉ๋ฒ
๋์ด์ indexPath์ ์์กดํ์ง ์์ต๋๋ค.
์์ง๊น์ง tableview, collectionviewdatasource๋ฅผ ์จ์จ ์ฌ๋์ด ์ฒ์ diffabledatasource๋ฅผ ๊ณต๋ถํด๋ณธ ๊ฒฐ๊ณผ ๋ฌ๋ผ์ง ์ ์ ๊ณ ์ฐฐํด๋ณด๊ณ ์ ํ๋ค.
์ฌํ tavleview, collectionvie์ indexPath์ ์์กดํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ธ์ํ๊ณ UI์ ๋ ๋๋งํ์๋๋ฐ ์ด ์์
์ ์์กด๋๋ฅผ ๋ฎ์ท๋ค๋ ์๊ฐ์ด ๋ ๋ค. cell์ ์ญ์ ๋ฐ ์์ , ์ถ๊ฐ ๋ฑ์ ์์
์ด ๋น๋ฒํ๊ฒ ์ด๋ค์ง ๊ฒฝ์ฐ cell์ ๊ฐ๋ณ indexpath ํ๋กํผํฐ๋ฅผ ํตํด ๊ฐ๊ฐ์ cell์ ๊ตฌ๋ถํ์๋ค. ํ์ง๋ง ์ด์ indexpath๊ฐ ์๋ model data๊ฐ Hashable ํ๋กํ ์ฝ์ ์ค์ํ์ฌ ๋ชจ๋ธ ๋ฐ์ดํฐ๊ฐ ๊ตฌ๋ณํ identifier๋ฅผ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ ์์กด๋๊ฐ ๋ฎ์์ง๊ฒ ๊ฐ๋ค. ๋์ฑ์ด ๋ง๋ฒ ๋ฉ์๋(reloadData)๋ฅผ ๋ ์ด์ ์ฐ์ง ์์๋ ๋๋ค๋ ์ ์ด ์์คํ
์ ๋ถ๋ด์ด ๋์ด์ง ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ ๋ค. ์ฑ๋ฅ์ 2021 WWDC ์์์ ๋ด์ผ ์๊ฒ ์ง๋ง ํํ 3๊ฐ์ง ๋จ๊ณ๋ง ์งํจ๋ค๋ฉด controller์ UI ๊ฐ์ sync๋ฅผ ๊ณ ๋ คํ์ง ์์๋ ๋๋ค๋ ์ ๋ฑ๋ฑ์์ ํจ์จ์ ์ธ ๊ฐ์ ์ฑ
์ด๋ผ๊ณ ์๊ฐ ๋ค์๋ค.
Main queue์์ ๋ฒ์ด๋ Background queue์์๋ ์์ ํ๊ฒ ์๋ํ ์ ์๋๋ก ๊ณ ์ํ๋ค๊ณ ํ์ ๋ค .. ! ํ์ง๋ง Main queue์์๋ง ๋๋ Background queue์์๋ง ๋ฐฐํ์ ์ผ๋ก ํธ์ถํด์ผ ํ๋ค ์๊ทธ๋ฌ๋ฉด log ์ฐํ ๊ฒ
์ถ๊ฐ) Airdrop extension์ ์ฌ์ฉ๋จ !!