Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBP-4700 Pagination 트리거 로직을 개선하고 Secion 단위가 아닌 List 단위로 콜백받을 수 있도록 변경해요. #43

Closed
wants to merge 10 commits into from

Conversation

ppth0608
Copy link
Contributor

배경

  • 기존 KarrotListKit의 Pagination 처리 방식은 UICollectionViewDelegatewillDisplayItem:at 함수를 이용하여 마지막 아이템의 index보다 threshold 만큼의 차이가 나는 아이템이 노출되었을때 NextBatchTrigger 이벤트를 콜백하는 방식으로 구현되어 있어요.
  • 위 방식은 Cell의 height에 따라 Trigger 시점이 달라질 수 있기 때문에 의도하지 않는 시점에 다음 페이지를 요청하는 문제가 존재해요.
  • 이를 개선하여, 노출된 아이템의 index 기반이 아닌 ContentOffset 기반으로 리스트의 끝 위치로부터 설정한 위치만큼 도달하게되면 트리거 이벤트를 콜백하는 방식으로 수정하였어요.

변경사항

  • 기존 Next Batch Trigger 로직 제거 및 새 로직 추가

리뷰노트

  • 코드 리뷰는 커밋 단위로 보는 것이 편해요.
  • 새롭게 변경된 방식은 다음과 같이 사용할 수 있어요.
  1. 트리거 시점을 설정하기 위해 CollectionViewAdapterConfiguration의 leadingScreensForBatching 속성을 설정합니다. leadingScreensForNextBatching 값은 리스트의 높이(혹은 너비) 값의 배수만큼 떨어져있음을 의미합니다)

    let configuration = CollectionViewAdapterConfiguration(
      leadingScreensForNextBatching: 1.0 // default is 2.0
    )
    let adapter = CollectionViewAdapter(
      configuration: configuration
      ...
    )
  2. NextBatchFetchDecisionProvider 프로토콜을 구현하여 Next Batch Trigger 이벤트를 콜백받을지 여부를 반환합니다. 혹은 미리 구현된 AnyNextBatchFetchDecisionProvider 구현체를 사용할 수 있습니다.

    class SomeNextBatchFetchDecisionProvider: NextBatchFetchDecisionProvider {
    
      func shouldBeginNextBatchFetch() -> Bool {
        // return a boolean value indicating whether the next batch fetch should start.
        return true // or false
      }
    }
    
    // or using type-erased wrapper for NextBatchFetchDecisionProvider
    
    let decisionProvider = AnyNextBatchFetchDecisionProvider {
        // return a boolean value indicating whether the next batch fetch should start.
        return true // or false
    }
  3. List.onNextBatchTrigger 이벤트를 주입합니다.

    List(sections: [])
      .onNextBatchTrigger(
        decisionProvider: decisionProvider,
        handler: { _ in
          // Closure Trigger when reached bottom of list.
        }
      )
  4. 데이터 가져오기를 마친 후에는 KarrotListKit에 프로세스가 완료되었음을 알리는 것이 매우 중요합니다. 그러기 위해서는 NextBatchContextcompleteBatchFetching() 메소드를 호출해야 합니다. 이렇게 하면 전체 일괄 가져오기 메커니즘이 동기화 상태로 유지되고 다음 일괄 가져오기 주기가 발생할 수 있습니다.

    List(sections: [])
      .onNextBatchTrigger(
        decisionProvider: decisionProvider,
        handler: { event in
          let nextBatchContext = event.context
          fetchNextPage() {
            nextBatchContext.completeBatchFetching()
          }
        }
      )

@ppth0608 ppth0608 force-pushed the feature/ben/MBP-4700-next-batch-trigger branch from 5f7efe5 to defa251 Compare June 3, 2024 12:44
@ppth0608
Copy link
Contributor Author

논의 후 인터페이스가 많이 변경되어 Close하고 재차 올릴께요!

@ppth0608 ppth0608 closed this Jun 20, 2024
@OhKanghoon OhKanghoon deleted the feature/ben/MBP-4700-next-batch-trigger branch June 21, 2024 10:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants