CollectionView Diffable Data Source scrolling unexpectedly

My app displays comments, and users can like these comments. The app uses a diffable data source, and when I click on the last row to like the comment, the collection view scrolls to the centre.
Here is a video of the issue
Code Block
 https://streamable.com/9vijyx 

(sorry about he music, my baby daughter was dancing)
Here is some of my code that I think is relevant - you'll see by the comments that I have tried to fix this issue by storing the content offset but it feels VERY hacky. Any help would be greatly appreciated.

Code Block fileprivate func configureDataSource() {
self.datasource = UICollectionViewDiffableDataSource<Section, PostDetail>(collectionView: self.collectionView) {
(collectionView: UICollectionView, indexPath: IndexPath, userComment: PostDetail) -> UICollectionViewCell? in
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PostDetailCell.reuseIdentifier, for: indexPath) as? PostDetailCell else { fatalError("Cannot create cell")}
cell.user = self.user
cell.postDetail = userComment
cell.likeCommentDelegate = self
return cell
}
var snapshot = NSDiffableDataSourceSnapshot<Section, PostDetail>()
snapshot.appendSections([.main])
snapshot.appendItems(self.userComments)
self.datasource.apply(snapshot, animatingDifferences: true)
}
fileprivate func applySnapshot() {
//Any updates to the snapshot, including likes, will
//scroll the collectionView, but we want to stay on the cell
// the user has liked, not scroll, so we store the offset.
//This is then restored after all updates are complete.
//let contentOffset = self.collectionView.contentOffset
var snapshot = NSDiffableDataSourceSnapshot<Section, PostDetail>()
snapshot.appendSections([.main])
snapshot.appendItems(self.userComments)
self.datasource.apply(snapshot, animatingDifferences: true)
//self.collectionView.contentOffset = contentOffset
}
func handleCommentLike(cell: PostDetailCell) {
print("handle like comment")
guard let indexPath = self.collectionView.indexPath(for: cell) else { return }
if self.userComments[indexPath.row].like == nil {
let userId = user.id
let postId = feedItem.post.id
let commentId = self.userComments[indexPath.row].comment.id
let timestamp = Int(Date().timeIntervalSince1970)
let commentLike = CommentLike(postId: postId, userId: userId, commentId: commentId, createdTimestamp: timestamp)
self.viewModel.createLike(commentLike: commentLike) { result in
switch result {
case .success(let commentLike):
print("\(commentLike)")
//
self.userComments[indexPath.row].like = commentLike
self.applySnapshot()
case .failure(let error):
print("Error creating like: \(error)")
}
}
} else {
guard let like = self.userComments[indexPath.row].like else { return }
self.viewModel.un(commentLike: like) { result in
switch result {
case .success(let like):
print(like)
self.userComments[indexPath.row].like = nil
self.applySnapshot()
case .failure(let error):
print(error)
}
}
}
}


Replies

@mickeysox

Did you ever figure this out?