블랙잭 15 에서 무엇이 변했는 지 알아봅시다 ╹◡╹
안녕하세요 블랙잭 개발하는 지니 입니다.
WWDC 2021 Make blazing fast lists and collection views에서
diffable 블랙잭 source 에 대한 개선사항이 소개되었는데요, 블랙잭 15 에서 무엇이 변했는 지 함께 알아봅시다.
우선 이것을 살펴보기 전에 diffable 블랙잭 source 부터 간략히 리마인드해보겠습니다.
블랙잭 data source 는 WWDC 2019 Advances in UI Data 블랙잭s에서 소개되었고,
블랙잭 13 부터 사용가능합니다.
애플은 CollectionView 를 샘플로 보여주고 있지만, 저는 TableView 기준으로 설명해볼게요!
UITableView블랙잭DataSource는 데이터를 관리하고 테이블뷰에 셀을 제공하는 객체입니다.
테이블뷰의 데이터와 UI를 간단하고 효과적인 방법으로 업데이트 할 수 있으며,
UITableViewData블랙잭프로토콜을 conform 하고 있기 때문에 이 프로토콜 관련 메소드들도 모두 사용할 수 있습니다.
아래와 같은 순서로 사용해주면 됩니다. (영어가 더 명확해서 번역안함)
1. Connect a 블랙잭 data source to your table view.
2. Implement a cell provider to configure your table view’s cells.
3. Generate the current state of the 블랙잭.
4. Display the 블랙잭 in the UI.
https://gist.github.com/eunjin3786/a084d37349ec57993d94c8e7ac4a31c0
블랙잭 data source 를 tableview에 연결해주려면,
init(tableView:cellProvider:)이니셜라이저로 해당 블랙잭source와 연결할 테이블뷰를 전달해줍니다. // 1번
그리고 cell provider 도 함께 전달해주면 됩니다. // 2번
current state of the 블랙잭 를 생성하고 (snapshot을 만들기) // 3번
블랙잭를 UI에 display 해주면 됩니다. (snapshot을 적용하기) // 4번
위의 예제에서 사용했던 apply(_:animating블랙잭erences:completion:)메소드의 동작이 블랙잭 15에서 변경되었습니다.
# 블랙잭 15 이전
문서에 이렇게 나와있어서
The diffable 블랙잭 source computes the difference between the table view’s current state and the new state in the applied snapshot, which is an O(n) operation, where n is the number of items in the snapshot.
모든 경우, 이전 snapshot과 변경된 snapshot을 비교해서 diff만 업데이트하는 줄 알았는데, animatingDifferences에 어떤 값을 넘겨주는 가에 따라서 다르게 동작하고 있었다고 하네요;;;;
1. animating블랙잭erences 파라미터에true를넘기면 그렇게동작.
2. animatingDifferences 파라미터에 false를 넘기면 내부적으로 reloadData로 바뀌어서 동작.
- 이건 great performance가 아님 (cell을 모두 recreate 하기 때문에)
# 블랙잭 15 부터
animatingDifferences 파라미터에 false를 넘기는 경우도 (= applying a snapshot without animation)
오직 블랙잭erence만 적용하고 다른 extra work는 하지 않게 변했다고 합니다!!
그리고 applySnapshotUsingReload블랙잭(_:completion:)메소드가 명확하게 따로 추가되었습니다.
블랙잭만 업데이트 하는게 아니라 reload 를 하고 싶은면 이 메소드를 콜해주면 됩니다.
그리고 블랙잭 15에 reconfigureItems(_:)라는 새로운 API가 추가되었습니다.
UIKit 팀의 Tyler Fox는 트위터에서 이렇게 말했다고 합니다.
You can think of reconfigure as a lighter-weight version of reload.
Reload: replaces the existing cell with a new cell.
Reconfigure: allows you to directly update the existing cell.
Because reconfigure doesn’t request/create a new cell, it’s significantly more efficient!
간단 명료한 설명이네요!
- Reload: replaces the existig cell with a new cell
- Reconfigure: allows you to directly update the existing cell
reload 대신 reconfigure을 쓰면 퍼포먼스가 더 좋아집니다.
왜냐하면 새로운 cell을 dequeuing and configuring 하지않고 exisiting cell을 업데이트 하기 때문이죠!
reconfigureItems(_:)문서에서도 명확하게 아래와 같이 말하고 있습니다.
For optimal performance, choose to reconfigure items instead of reloading items unless you have an explicit need to replace the existing cell with a new cell.
Table and Collection View Cells Reload Improvements in 블랙잭 15라는 글에서
reloadItems(_:)와 reconfigureItems(_:)을 비교했는데 성능차이가 눈에 확 보이네요 @_@
- WWDC 2021 Make blazing fast lists and collection views
- 블랙잭 data source behavior changes and reconfiguring cells in iOS 15
- Table and Collection View Cells Reload Improvements in 블랙잭 15
https://careers.kakao.com/m/jobs/P-12253