/ SDKs / iOS
SDKs
Chat SDKs iOS v4
Chat SDKs iOS
Chat SDKs
iOS
Version 4

Retrieve a list of channels

Copy link

You can retrieve a list of OpenChannel objects using the loadNextPage(completionHandler:) method of an OpenChannelListQuery instance.

On the other hand, a list of GroupChannel objects can be retrieved through GroupChannelCollection and its loadMore() method. First, set GroupChannelListQueryParams and create a GroupChannelListQuery instance. Then pass the query to the parameter in createGroupChannelCollection(). To retrieve a list of certain group channels such as public group channels or Supergroup channels, use the the list query's filters as described at the bottom of this page.

Note: You can also search for specific open channels and group channels with keywords and filters.


Open channels

Copy link

Create an OpenChannelListQuery instance to retrieve a list of open channels. If you wish to filter open channels, set OpenChannelListQueryParams and pass it into createOpenChannelListQuery(params:). After a list of open channels is successfully retrieved, you can access the data of each open channel from the result list through the channels parameter of the callback handler.

class CustomViewController: ViewController {
    var query: OpenChannelListQuery?

    func createQuery() {
        self.query = OpenChannel.createOpenChannelListQuery()
    }

    func loadNextPage() {
        self.query?.loadNextPage { channels, error in
            guard error == nil else {
                // Handle error.
                return
            }

            // A list of open channels is retrieved.
        }
    }
}

Group channels

Copy link

A group channel list view can be drawn with a GroupChannelCollection instance. In order to create a group channel collection, set GroupChannelListQueryParams to filter and sort group channels, which is used in createMyGroupChannelListQuery(). Then pass the query instance in the createGroupChannelCollection() method when creating a group channel collection.

To retrieve group channels in the collection, call hasMore first to check whether there are more channels to load. If so, call loadMore().

To learn more about how the collection works, see Group channel collection under Local caching.

let qParams = GroupChannelListQueryParams()
qParams.includeEmptyChannel = true
qParams.order = .chronological
let query = GroupChannel.createMyGroupChannelListQuery(params: qParams)
let collection = SendbirdChat.createGroupChannelCollection(query: query)
// Call hasNext first to check if there are more channels to load.
if collection?.hasNext == true {
    collection?.loadMore(completionHandler: { channels, error in
        guard error == nil else {
            // Handle error.
            return
        }
        // Add channels to your data source.
    })
}

Filter group channels using GroupChannelListQuery

Copy link

When creating a GroupChannelCollection instance, you need to create a GroupChannelListQuery instance first and set its params. The params in GroupChannelListQuery works much like other search options, retrieving group channels based on the values set to the params. For example, use myMemberStateFilter when searching for only the group channels that the current user belongs to, and publicChannelFilter when searching for all public group channels regardless of the current user's membership state. To retrieve a list of Supergroup channels only, you can set the superChannelFilter property to SuperChannelFilter.super.

Once the params within GroupChannelListQuery is set, you can pass the query instance to query in createGroupChannelCollection() as instructed in the previous section.

// Create a GroupChannelListQueryParams instance.
var qParams = GroupChannelListQueryParams()
qParams.includeEmptyChannel = true
qParams.order = .chronological
qParams.publicChannelFilter = .public       // Retrieve public group channels.
qParams.superChannelFilter = .super     // Retrieve Supergroup channels.

GroupChannelListQueryParams

Copy link

This table only contains properties shown in the code above. To see the comprehensive list of all available methods and properties, see GroupChannelListQueryParams in API reference.

Property nameTypeDescription

includeEmptyChannel

Bool

Determines whether the list includes empty group channels that don't show any messages sent before the current user joins it because the chat history option is turned off. You can turn on or off the chat history option under Settings > Chat > Channels > Group channels on Sendbird Dashboard.

order

enum

Determines how to order the channels. Acceptable values are chronological, latestLastMessage, channelNameAlphabetical, and channelMetaDataValueAlphabetical. channelMetaDataValueAlphabetical works in conjunction with metaDataOrderKeyFilter. (Default: chronological)

publicChannelFilter

enum

Determines which group channels to include in the list based on their isPublic value. Acceptable values are private, public, and all. (Default: all)

superChannelFilter

enum

Determines which group channels to include in the list based on their isSuper value. Acceptable values are nonSuper, super, and all. (Default: all)