Diffable DataSource

Youn·2022년 6월 5일
0

iOS-Swift

목록 보기
10/11
post-thumbnail

WHY ?

  • Datasource 를 관리하는 Controller 에게 웹서비스로부터 응답이 왔을 때 UI에게 변경을 알림
  • 에러 발생
  • reloadData 를 통해 해결 가능하지만 애니메이션적용 X → 사용자 경험을 해침
  • ➡️ Data Controller 와 UI 모두 그들만의 one version of the truth 를 가지고 있음 → truth들이 다를경우 문제 발생
  • 즉, centralized 된 truth 가 없음

Diffable DataSource

  • iOS13+
  • UI state 에 선언적 접근
  • snapshot = 현재 UI state 의 truth
  • sections 과 items 에 Unique Identifiers 부여, IndexPath ❌
    - 빠른 속도
    • 간결성
    • 잘못된 indexpath 로 crash 날 확률이 줄어듬
    • 자동으로 cell 이 사라지고 생기는 애니메이션이 동작함
  • Generic Class (Hashable 준수하는 타입 넣어서 사용)

Example

  1. 데이터의 변경
  2. 새로운 snapshot 생성
  3. snapshot 에 section 과 item(변경된 데이터) 추가
  4. datasource 에 해당 snapshot apply

apply(animatingDifferences)

Prior to iOS 15, passing true to animatingDifferences would apply the diff and animate updates in the UI, while passing false was equivalent to calling reloadData(). As of iOS 15, applying a snapshot using this API will always perform a diff
참고

  • iOS 15 이전 animationgDifference
    • true : diff
    • false : reload
  • iOS 15 이후 : 둘다 diff

참고
https://zeddios.tistory.com/1197
https://ios-development.tistory.com/717
https://developer.apple.com/videos/play/wwdc2019/220/
https://brunch.co.kr/@eunjin3786/245

profile
youn

0개의 댓글