Reputation: 629
I am currently utilizing the UICollectionViewDiffableDataSource to develop a Chat screen, where I retrieve data from an API.
Upon making an initial API call, I receive 20 conversations which are loaded into the UICollectionView. Upon loading, I am making the scroll position is set to the end.
However, when I scroll up to reach the top and trigger pagination, the new response is inserted into the snapshot.
Ideally, the scroll position should remain unchanged, but currently, it resets to the top.
Below is a snippet of my code for applying to the dataSource. I am seeking guidance on how to prevent the scroll from resetting to the top during pagination.
func applyInitialSnapshot(scrollToBottom: Bool) {
var snapshot = NSDiffableDataSourceSnapshot<Section, MDLMessage>()
let data = viewModel.objConversation?.groupMessages ?? []
for val in data {
let section = Section.date(val.dateString)
snapshot.appendSections([section])
snapshot.appendItems(val.value)
}
dataSource.apply(snapshot, animatingDifferences: scrollToBottom) { [weak self] in
if scrollToBottom {
DispatchQueue.main.async {
self?.scrollToLast()
}
}
}
}
enum Section: Hashable {
case date(String)
}
here scrollToBottom
I am passing true
on initial and later during pagination passing false
.
Upvotes: 0
Views: 49