Home
/
Chat
/
iOS
/
Local caching

Group channel collection

A SBDGroupChannelCollection 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.

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


Create a collection

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

First, create a SBDGroupChannelListQuery instance through the 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 the loadMore().

SwiftObjective-C
class ChannelListViewController: UIViewController {
    // ...

    func createGroupChannelCollection() {
        // First, create a SBDGroupChannelListQuery in the SBDGroupChannelCollection.
        let query = SBDGroupChannel.createMyGroupChannelListQuery()
        query?.includeEmptyChannel = true
        query?.order = .chronological // The value can be ` chronological`, `latestLastMessage`, `channelNameAlphabetical`, and `channelMetaDataValueAlphabetical`.
        // You can add other query setters.

        // Create a SBDGroupChannelCollection instance.
        self.collection = SBDGroupChannelCollection(query: query)
    }

    // ...
}

Pagination

A SBDGroupChannelCollection retrieves more channels to display in the view through the loadMore() method in Swift and the loadMoreWithCompletionHandler: in Objective-C.

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() method in Swift and the loadMoreWithCompletionHandler: in Objective-C will fetch them.

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

SwiftObjective-C
class ChannelListViewController: UIViewController {
    // ...

    func loadMore() {
        if let collection = self.collection {
            // Check whether there are more channels to load before calling the 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 SBDGroupChannelCollectionDelegate 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.

SwiftObjective-C
class ChannelListViewController: UIViewController {
    // ...

    func createGroupChannelCollection() {
        // ...

        self.collection = SBDGroupChannelCollection(query: query)
        self.collection?.delegate = self

        // ...
    }

    // ...
}

extension ChannelListViewController: SBDGroupChannelCollectionDelegate {
    func channelCollection(_ collection: SBDGroupChannelCollection, context: SBDChannelContext, addedChannels channels: [SBDGroupChannel]) {
        // Add new channels to your data source.
    }

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

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

Dispose of the collection

The dispose in Swift and disposeCollection in Objective-C should be called when you need to clear the current channel list view.

SwiftObjective-C
class ChannelListViewController: NSObject {
    // ...

    func disposeCollection() {
        self.collection!.dispose()
    }

    // ...
}