/ SDKs / .NET
SDKs
Chat SDKs .NET v4
Chat SDKs .NET
Chat SDKs
.NET
Version 4

Message collection

Copy link

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

Copy link

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.

SbMessageListParams messageListParams = new SbMessageListParams();

SbMessageCollectionCreateParams createParams = new SbMessageCollectionCreateParams(messageListParams)
{
    StartingPoint = STARTING_POINT,
    MessageCollectionHandler = new SbMessageCollectionHandler
    {
        OnMessagesAdded = (inContext, inAddedMessages) => 
        {
            /* Add the messages to your data source. */
        }
    }
};

SbMessageCollection messageCollection = groupChannel.CreateMessageCollection(createParams);

Starting point

Copy link

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

Copy link

After creating a SbMessageCollection instance, initialize the instance through the Initialize() method.

SbMessageCollectionInitHandler messageCollectionInitHandler = new SbMessageCollectionInitHandler
{
    OnApiResult = (inMessages, inError) =>
    {
        // Messages are retrieved from the Sendbird server through API.
    }
};

messageCollection.Initialize(messageCollectionInitHandler);

Pagination

Copy link

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.

cc
if (messageCollection.HasPrevious)
{
    // Load the previous messages
    // when the scroll reaches the first message in the chat view.
    messageCollection.LoadPrevious((inMessages, inError) =>
    {
        if (inError != null)
            return; //Handle error.
    });
}

Message events

Copy link

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.

HandlerCalled when

OnMessagesAdded()

- A new message is created as a real-time event.
- New messages are fetched during changelog sync.

OnMessagesUpdated()

- A message is updated as a real-time event.
- Message update is detected during changelog sync.
- The send status of a pending message changes.

OnMessagesDeleted()

- A message is deleted as a real-time event.
- Message deletion is detected during changelog sync.
- The value of the SbMessageListParams setter such as CustomTypes changes.

OnChannelUpdated()

- The channel information that is included in the user's current chat view is updated as a real-time event.
- Channel info update is detected during changelog sync.

OnChannelDeleted()

- The current channel is deleted as a real-time event.
- Channel deletion is detected during changelog sync.
* In both cases, the entire view should be disposed of.

OnHugeGapDetected()

- A huge gap is detected through background sync. In this case, you need to dispose of the view and create a new SbMessageCollection instance.


Huge gap

Copy link

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.

messageCollection.Dispose();

Dispose of the collection

Copy link

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.

messageCollection.Dispose();