UICollectionView와 DiffableDataSource를 기본적으로 알아야 합니다.
iOS 13에서 소개된 diffable data source는 UI state를 snapshot data type
을 추가함으로써 단순화 한다.
Snapshot은 UI state를 유니크한 section과 item identifier를 통해 캡슐화한다.
따라서 UICollectionView를 update할 때, 먼저 새로운 snapshot을 만든 뒤에 현재 UI 상태로 populate 한 뒤, data source에 적용한다.
diffable data source는 differences를 계산하고, 자동적으로 animate해, 개발자가 추가적인 작업을 하지 않아도 된다.
Advanced in UI Data Source를 보면 더 자세하게 알 수 있다.
iOS14에서 Diffable Data Source의 foundation을 두 feature로 나눴다.
section snapshot은 UICollectionView의 single section에 대한 data를 캡슐화한다.
이 enhancement에는 두 가지 이유가 있다.
snapshot
은 iOS13에서 소개된 section과 item identifier를 포함하는 단어이고
section snapshot
은iOS 14 타입이다.
우리는 section snapshot을 content에 추가하기 위해 append
API를 사용한다.
optional parant parameter가 안주어지면 parent이고, 주어지면 child인 hierachical한 data를 만들 수 있다.
UICollectionViewDiffableDataSource에는 2개의 API가 추가되었다.
apply
메소드는 section snapshot과 section identifier를 매개변수로 같는다.append
API를 사용하여 root에 item을 추가하고, food-parent에 emoji-child를 hierachical하게 추가한다.
다음 코드는 parent에 related된 모든 child section snapshot을 가져온다.
expansion은 section snapshot state 의 part로 관리된다.
parent expension state를 통해 child가 initially visible할지를 결정할 수 있다.
특정한 parent가 절대 collapse하지 않는 디자인을 만들어야할 때가 있다. 따라서 Diffable Data Source는 user interaction에 의한 programmatic한 control을 지원하는 새로운 API를 제공한다.
SectionSnapshotHandlers 프로퍼티는 struct타입으로 5가지 Optional Closure를 가지고 있다.
Collapsing을 방지하기 위해 shouldCollapseItem을 false로 반환할 수 있다.
advanced Diffable data source는 우리의 콜렉션 뷰의 data를 unique item identifiers로 모델링 할 수 있게 한다.
Automatic한 reordering을 도와준다.
user가 reordering interaction을 끝내면 didReorder
closure가 불린다.
reordering feature를 enable 하기 위해서는 canReorderItem
과 didReorder
클로저가 제공되어야 한다.
view에서 items를 reorder한 후 변경사항을 설명하는 transaction이다.
initialSnapshot
은 update가 적용되기 전의 Diffable Data Source의 state이다.
finalSnapshot
은 update 후 Diffable Data Source의 state이다.
array같은 데이터를 사용할 경우 difference
에 직접적으로 데이터를 적용 해라.
sectionIdentifier
는 이 sectionTransaction이 어떤 section에 적용될지를 가르킨다.