Message collection
A MessageCollection
instance allows you to create a chat view that includes all up-to-date data. This page explains how to make a view using the collection.
Create a collection
First, create a MessageCollection
instance. Here, you need to set a MessageListParams
instance to determine the message order and the starting point of the message list in the chat view.
Starting point
The reference point for message retrieval in a chat view is startingPoint
. This should be specified as a timestamp.
By default, startingPoint
is set to SendbirdChat.maxInt
, displaying the most recent messages first. However, if the startingPoint
is set to the message last read by the user, it will display messages from where the user last read.
Initialization
After creating a MessageCollection
instance, initialize the instance through the initialize()
method.
Pagination
Use the loadPrevious()
and loadNext()
methods to retrieve messages from the previous page and the next page.
When the loadPrevious()
method is called, the Chat SDK first checks whether there're messages to load from the previous page through hasPrevious
. The same goes for hasNext
and loadNext()
.
These methods have to be called during initialization as well.
Message events
Use MessageCollectionHandler
to determine how the client app reacts to message-related events.
The following table shows possible cases where each event handler can be called.
Handler | Called when |
---|---|
onMessagesAdded() | - A new message is created as a real-time event. |
onMessagesUpdated() | - A message is updated as a real-time event. |
onMessagesDeleted() | - A message is deleted as a real-time event. |
onChannelUpdated() | - The channel information that is included in the user's current chat view is updated as a real-time event. |
onChannelDeleted() | - The current channel is deleted as a real-time event. |
onHugeGapDetected() | - A huge gap is detected through background sync. In this case, you need to dispose of the view and create a new |
Huge gap
A gap can be created for many reasons, such as when a user has been offline for days. When the client app comes back online, the Chat SDK checks with the Sendbird server to see if there are any new messages. If it detects that more than 300 messages are missing in the local cache compared to the Sendbird server, the SDK determines that there is a huge gap and onHugeGapDetected()
is called.
Note: To adjust the number of missing messages that result in a call for
onHugeGapDetected()
, contact our support team.
In this case, the existing MessageCollection
instance should be cleared through the dispose()
method and a new one must be created for better performance.
Dispose of the collection
The dispose()
should be called when you destroy the chat screen to notify the SDK that this collection is no longer visible.
For example, this should be used when the current user exits from the chat screen.