Chat iOS v4
Chat iOS
Chat
iOS
Home
/
Chat
/
iOS
/
Local caching
This is the new Docs for Chat SDK v4 beta for iOS. To see the previous Docs, click here.

Group channel collection

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


Create a collection

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

First, create a GroupChannelListQuery instance through the SendbirdChat.createMyGroupChannelListQuery() method and its 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 loadMore(completionHandler:).

class ChannelListViewController: UIViewController {
    func createGroupChannelCollection() {
        // First, create a GroupChannelListQuery instance.
        let query = GroupChannel.createMyGroupChannelListQuery()
        query.includeEmptyChannel = true
        query.order = .chronological // Acceptable values are chronological,
        // latestLastMessage, channelNameAlphabetical, and channelMetaDataValueAlphabetical.
        // You can add other query setters.

        // Then create a GroupChannelCollection instance.
        self.collection = SendbirdChat.createGroupChannelCollection(query: query)
    }
}

Pagination

A GroupChannelCollection instance retrieves more channels to display in the view through the loadMore(completionHandler:) method.

Whenever a scroll reaches the bottom of the channel list view, the loadMore() method should be called and the hasMore method will first check if there are more channels to load. If true, loadMore(completionHandler:) will fetch them.

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

class ChannelListViewController: UIViewController {

    func loadMore() {
        // Check whether there are more channels to load before calling loadMore().
        if collection.hasMore {
            collection.loadMore { channels, error in
                if error != nil {
                    // Handle error.
                    return
                }

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

}

Channel events

Use the GroupChannelCollectionDelegate methods 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

addedChannels

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

updatedChannels

- 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 through changelog sync.

deletedChannelURLs

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

class ChannelListViewController: UIViewController {
    func createGroupChannelCollection() {
        self.collection = SendbirdChat.createGroupChannelCollection(query: query)
        self.collection.delegate = self
    }
}

extension ChannelListViewController: GroupChannelCollectionDelegate {
    func channelCollection(_ collection: GroupChannelCollection, context: ChannelContext, addedChannels channels: [GroupChannel]) {
        // Add new channels to your data source.
    }

    func channelCollection(_ collection: GroupChannelCollection, context: ChannelContext, updatedChannels channels: [GroupChannel]) {
        // Update the existing channels in your data source.
    }

    func channelCollection(_ collection: GroupChannelCollection, context: ChannelContext, deletedChannelURLs: [String]) {
        // Delete the channels with the matching deletedChannelURLs
        // from your data source.
    }
}

Dispose of the collection

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

class ChannelListViewController: NSObject {
    func disposeCollection() {
        self.collection.dispose()
    }
}