Message collection
An SbMessageCollection
instance allows you to swiftly create a chat view that includes all data. This page explains how to make a view using the collection.
Create a collection
First, create an SbMessageCollection
instance. Here, you need to set an SbMessageListParams
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 long.MaxValue
, 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 SbMessageCollection
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 SbMessageCollectionHandler
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 SbMessageCollection
instance should be cleared through the Dispose()
method and a new one must be created for better performance.
Dispose of the collection
The Dispose()
method should be called when you need to clear the current chat view. For example, this can be used when the current user leaves the channel.