3

I have developed a Chat App, where I used MessageKit for my iOS app. My issues is when I open any Chat screen, messageCollectionView starts from top ,showing old chats. I need to start the collectionView from bottom as soon as I open any chat screen. I have tried :

messagesCollectionView.scrollToBottom(animated: true)

but there is a performance impact (in case there and more than hundreds/thousands of messages).

Is there any way if inverting the collection view, or starting to add the cells from bottom and then top.

TMirza
  • 47
  • 5
  • 1
    fetch messages in form of small chunks.Load 20-25 latest messages form DB first time. fetch next chunks of data at the time of scrolling. it will improve your performance and User experience. – SGDev Nov 08 '19 at 06:52
  • Thanks for the reply, but I want to show the bottom of collectionView with the recent text message shown at bottom. Just like other messengers we have (FB, Insta etc) – TMirza Nov 08 '19 at 07:20
  • fetch data in form of descending order means, last message at the end of the dataSource and when you get next chunks of data you can update(add in) the dataSource(in front). – SGDev Nov 08 '19 at 07:33

1 Answers1

2

The above suggestions in comments were more robust and correct implementation.


alternatively you can try this:

//After reloading
DispatchQueue.main.async {
    self.messagesCollectionView.scrollToItem(at: IndexPath(row: 0, section: self.messages.count - 1), at: .top, animated: false)
   }
Suresh Mopidevi
  • 919
  • 3
  • 9
  • 24