Swift How to Show Most Recent Firebase Messages Without Glitchy Scrolling to Last Index

65 Views Asked by At

I am trying to figure how to show just the last messages like instagram and Facebook does when you click on a chat (in my case just one between 2 users).

After I observe all the messages, I then execute this which I think is standard

DispatchQueue.main.async(execute: {
     //scroll to the last index
     self.collectionView.reloadData()
     let indexPath = IndexPath(item: self.messages.count - 1, section: 0)
     self.collectionView?.scrollToItem(at: indexPath, at: .bottom, animated: false)
})

Unfortunately this will show scroll through the messages in a very glitchy way and does so each time I open a chat. I haven't been able to find a way to show the most recent messages online. Everyone seems to be using scrollToItem (animated doesn't matter because it looks choppy either way). How do I just show the most recent messages every time I click on a chat??

0

There are 0 best solutions below