Home
/
Chat
/
Android
/
Local caching

Group channel collection

A GroupChannelCollection allows you to swiftly create a channel list view that doesn’t miss any channel-related events. This page explains how to make a view using the collection.

Note: Understand the differences between local caching and SyncManager, and learn how to migrate Group channel collection.


Create a collection

You can create a GroupChannelCollection instance through the createGroupChannelCollection() method.

First, create a GroupChannelListQuery instance through the createMyGroupChannelListQuery() method and query setters. This will determine which channel to include in the channel list and how to order it.

Once the collection is created, you should call the loadMore().

private void createGroupChannelCollection() {
    // First, create a GroupChannelListQuery in the GroupChannelCollection.
    GroupChannelListQuery query = GroupChannel.createMyGroupChannelListQuery();
    query.setIncludeEmpty(true);
    query.setOrder(GroupChannelListQuery.Order.CHRONOLOGICAL); // The value can be ` CHRONOLOGICAL`, `LATEST_LAST_MESSAGE`, `CHANNEL_NAME_ALPHABETICAL`, and `METADATA_VALUE_ALPHABETICAL`.
    // You can add other query setters.

    // Create a GroupChannelCollection instance.
    final GroupChannelCollection collection = new GroupChannelCollection.Builder(query).build();
}

Pagination

A GroupChannelCollection can retrieve channels through the loadMore() method to display in the view.

Whenever a scroll reaches the bottom of the channel list view, the loadMore() is called and the hasMore() will first check if there're more channels to load. If true, the loadMore() will fetch them.

The loadMore() should also be called after you've created a GroupChannelCollection.

private void loadMore() {
    // Check whether there are more channels to load before calling the loadMore().
    if (collection.hasMore()) {
        collection.loadMore(new OnChannelLoadResultHandler() {
            @Override
            public void onResult(@Nullable List<GroupChannel> channels, @Nullable SendBirdException e) {
                if (e != null) {
                    // Handle error.
                    return;
                }

                // Add channels to your data source.
            }
        });
    }
}

Channel events

Use the setGroupChannelCollectionHandler() to determine how the client app would react to channel-related events.

This is called whenever a new channel is created as a real-time event or changelog sync is prompted when the client app is back online.

The following table shows possible cases where each event handler can be called.

EventCalled when

onChannelAdded()

- A new group channel is created as a real-time event.
- New group channels are fetched through changelog sync.

onChannelUpdated()

- The channel information that is included in the user's current chat view is updated as a real-time event.
- Updated channel information is fetched through changelog sync.

onChannelDeleted()

- A group channel is deleted as a real-time event.
- A channel deletion event is fetched through changelog sync.

collection.setGroupChannelCollectionHandler(new GroupChannelCollectionHandler() {
    @Override
    public void onChannelsAdded(@NonNull GroupChannelContext context, @NonNull List<GroupChannel> channels) {
        // Add new channels to your data source.
    }

    @Override
    public void onChannelsUpdated(@NonNull GroupChannelContext context, @NonNull List<GroupChannel> channels) {
        // Update the existing channels in your data source.
    }

    @Override
    public void onChannelsDeleted(@NonNull GroupChannelContext context, @NonNull List<String> deletedChannelUrls) {
        // Delete the channels with the matching deletedChannelUrls from your data source.
        }
    });
}

Dispose of the collection

The dispose() should be called when you need to clear the current channel list view.

private void dispose() {
    collection.dispose();
}