// willDisplayCell
collectionView.rx.willDisplayCell
.filter { $0.at.section == 1 }
.map {
let currentItem = $0.at.item
if (currentItem + 1) % 6 == 0 && currentItem != 0 {
return currentItem / 6 + 1
}
return nil
}
.compactMap { $0 }
.map { Reactor.Action.willDisplayCell($0) }
.bind(to: reactor.action)
.disposed(by: disposeBag)
func setUpCommentSection(_ currentPage: Int = 0) -> Observable<Mutation> {
let currentPage = currentPage
let query: [String: Int] = ["page": currentPage]
if currentState.loadedPage.contains(currentPage) { return .empty() }
return CommunityAPI.fetchCommunityComment(currentState.communityId, query)
.catch { _ in .empty() }
.flatMap { data -> Observable<Mutation> in
var commentItem = self.currentState.commentItem
let newComments = data.comments.filter { newComment in
!commentItem.contains(where: { $0?.commentId == newComment.commentId })
}
commentItem.append(contentsOf: newComments)
return .concat([
.just(.setCommentItem(commentItem)),
.just(.setCommentCount(data.commentCount)),
.just(.setLoadedPage(currentPage))
])
}
}
rxcocoa에 prefetchItem을 사용
willDisplayCell은 방출하는 데이터가 Cell, IndexPath로 이루어져 있는 반면 prefetchItems는 IndexPath만으로 이루어져 있어 효율적일것이라 생각
IndexPath 배열이 내려오고, 오름차순으로 내려오므로 가장 마지막 인덱스 값을 얻어와서 그 인덱스 값이 현재 데이터 소스의 마지막 인덱스일때 api를 호출하도록 구현
// PrefetchItems
collectionView.rx.prefetchItems
.compactMap(\.last?.item)
.map { Reactor.Action.prefetchItems($0) }
.bind(to: reactor.action)
.disposed(by: disposeBag)