brunch

You can make anything
by writing

C.S.Lewis

by 갬성개발자 Dec 28. 2021

Diffable Data Source in iOS 15

iOS 15 에서 무엇이 변했는 지 알아봅시다 ╹◡╹

안녕하세요 iOS 개발하는 지니 입니다.


WWDC 2021 > Make blazing fast lists and collection views 에서

diffable data source 에 대한 개선사항이 소개되었는데요, iOS 15 에서 무엇이 변했는 지 함께 알아봅시다.


우선 이것을 살펴보기 전에 diffable data source 부터 간략히 리마인드해보겠습니다.

diffable data source 는 WWDC 2019 > Advances in UI Data Sources 에서 소개되었고,

iOS 13 부터 사용가능합니다.

애플은 CollectionView 를 샘플로 보여주고 있지만, 저는 TableView 기준으로 설명해볼게요!



[1] UITableViewDiffableDataSource 


UITableViewDiffableDataSource 는 데이터를 관리하고 테이블뷰에 셀을 제공하는 객체입니다.


테이블뷰의 데이터와 UI를 간단하고 효과적인 방법으로 업데이트 할 수 있으며,

UITableViewDataSource 프로토콜을 conform 하고 있기 때문에 이 프로토콜 관련 메소드들도 모두 사용할 수 있습니다.


아래와 같은 순서로 사용해주면 됩니다. (영어가 더 명확해서 번역안함)


1. Connect a diffable 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 data.

4. Display the data in the UI.


https://gist.github.com/eunjin3786/a084d37349ec57993d94c8e7ac4a31c0


diffable data source 를 tableview에 연결해주려면,

init(tableView:cellProvider:) 이니셜라이저로 해당 datasource와 연결할 테이블뷰를 전달해줍니다. // 1번

그리고 cell provider 도 함께 전달해주면 됩니다.  // 2번


current state of the data 를 생성하고 (snapshot을 만들기) // 3번

data를 UI에 display 해주면 됩니다. (snapshot을 적용하기) // 4번



[2] apply snapshot without animation


위의 예제에서 사용했던  apply(_:animatingDifferences:completion:) 메소드의 동작이 iOS 15에서 변경되었습니다.


# iOS 15 이전


문서에 이렇게 나와있어서

The diffable data 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. animatingDifferences 파라미터에 true 넘기면 그렇게 동작.

2. animatingDifferences 파라미터에 false를 넘기면 내부적으로 reloadData로 바뀌어서 동작.

-> 이건 great performance가 아님 (cell을 모두 recreate 하기 때문에)



# iOS 15 부터

animatingDifferences 파라미터에 false를 넘기는 경우도  (= applying a snapshot without animation)

오직 difference만 적용하고 다른 extra work는 하지 않게 변했다고 합니다!!

 

그리고 applySnapshotUsingReloadData(_:completion:) 메소드가 명확하게 따로 추가되었습니다.

diff만 업데이트 하는게 아니라 reload 를 하고 싶은면 이 메소드를 콜해주면 됩니다.



[3] reconfigureItems


그리고 iOS 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 iOS 15 라는 글에서

reloadItems(_:)  와  reconfigureItems(_:) 을 비교했는데 성능차이가 눈에 확 보이네요 @_@


https://swiftsenpai.com/development/cells-reload-improvements-ios-15/



Reference


- WWDC 2021 > Make blazing fast lists and collection views 

- Diffable data source behavior changes and reconfiguring cells in iOS 15

- Table and Collection View Cells Reload Improvements in iOS 15





우리는 채용 중


https://careers.kakao.com/m/jobs/P-12253


매거진의 이전글 ReactorKit 테스트하기
작품 선택
키워드 선택 0 / 3 0
댓글여부
afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari