SyncManager SDKs iOS v1
SyncManager SDKs iOS
SyncManager SDKs
iOS
Version 1

Group channel collection

Copy link

A SBDGroupChannelCollection retrieves group channels from both the local cache and Sendbird server and lets you quickly create a channel list view without missing any channel-related updates. This page explains how to make a channel list view using the collection and serves as a migration guide.


Create a collection

Copy link

The SBDGroupChannelCollection is the local caching equivalent of SyncManager’s SBSMChannelCollection. The SBDGroupChannelCollection instance can be created through the createGroupChannelCollection() method.

Create a SBDGroupChannelListQuery instance through the createMyGroupChannelListQuery() method and query setters. This will determine the number of channels to retrieve for the channel list.

Local cachingSyncManager
let query = SBDGroupChannel.createMyGroupChannelListQuery()
query?.limit = CHANNEL_LIST_LIMIT

groupChannelCollection = SBDGroupChannelCollection(query: query!)
//SBDGroupChannelCollectionDelegate has to be implemented.
groupChannelCollection?.delegate = self 

Objective-C

Copy link
Local cachingSyncManager
SBDGroupChannelListQuery *query = [SBDGroupChannel createMyGroupChannelListQuery];
query.limit = CHANNEL_LIST_LIMIT;

self.groupChannelCollection = [[SBDGroupChannelCollection alloc] initWithQuery:query];
self.groupChannelCollection.delegate = self;

Channel events

Copy link

In local caching, the SBDGroupChannelCollectionDelegate is used to determine how the client app reacts to channel-related events.

The following table shows when to call each event handler.

EventCalled when

addedChannels

- A new group channel is created as a real-time event.
- New group channels are fetched by changelog sync.
- It replaces SyncManager's SBSMChannelEventAction.insert.

updatedChannels

- 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 during changelog sync.
- It replaces SyncManager's SBSMChannelEventAction.update and SBSMChannelEventAction.move.

deletedChannelUrls

- A group channel is deleted as a real-time event.
- A channel deletion event is fetched during changelog sync.
- It replaces SyncManager's SBSMChannelEventAction.remove.

SyncManager's SBSMChannelCollectionDelegate, which is used to handle real-time events, should be changed as shown in the code below.


Local cachingSyncManager
extension ViewController: SBDGroupChannelCollectionDelegate {
    func channelCollection(_ collection: SBDGroupChannelCollection, context: SBDChannelContext, addedChannels channels: [SBDGroupChannel]) {

    }

    func channelCollection(_ collection: SBDGroupChannelCollection, context: SBDChannelContext, updatedChannels channels: [SBDGroupChannel]) {

    }

    func channelCollection(_ collection: SBDGroupChannelCollection, context: SBDChannelContext, deletedChannelUrls: [String]) {

    }
}

Objective-C

Copy link
Local cachingSyncManager
// LocalCachingViewController.h
@interface LocalCachingViewController : UIViewController<SBDGroupChannelCollectionDelegate>

@end

// LocalCachingViewController.m
@implementation LocalCachingViewController

#pragma mark - SBDGroupChannelCollectionDelegate

- (void)channelCollection:(SBDGroupChannelCollection *)collection
                  context:(SBDChannelContext *)context
            addedChannels:(NSArray<SBDGroupChannel *> *)channels {

}

- (void)channelCollection:(SBDGroupChannelCollection *)collection
                  context:(SBDChannelContext *)context
          updatedChannels:(NSArray<SBDGroupChannel *> *)channels {

}

- (void)channelCollection:(SBDGroupChannelCollection *)collection
                  context:(SBDChannelContext *)context
       deletedChannelUrls:(NSArray<NSString *> *)deletedChannelUrls {

}

@end

List channels

Copy link

SyncManager's fetch method retrieves channels from the local cache and delivers them to the SBSMChannelCollectionDelegate instance. In local caching, SBDGroupChannelCollection can retrieve channels through two new interfaces, hasMore and loadMore.

By default, cached channels are listed in reverse chronological order, meaning the channel that most recently received a message appears at the top of the list. The channel order is automatically updated in the local cache when a new message arrives.

MethodsDescription

hasMore

- Checks if there are more channels to load.
- Called whenever a user scroll reaches the bottom of the channel list.

loadMore

- If hasMore is true, retrieves channels from the local cache to show in the channel list.
- Called whenever a user scroll reaches the bottom of the channel list.

Local cachingSyncManager
if groupChannelCollection.hasMore {
    groupChannelCollection.loadMore(completionHandler: { channels, error in
        // The channel list returns as a callback.
    })
}

Objective-C

Copy link
Local cachingSyncManager
if (self.groupChannelCollection.hasMore) {
    [self.groupChannelCollection loadMoreWithCompletionHandler:^(NSArray<SBDGroupChannel *> * _Nullable channels, SBDError * _Nullable error) {
        // The channel list returns as a callback.
    }];
}

Dispose of the collection

Copy link

SyncManager's SBSMChannelCollection has the remove() method which clears all the channels managed by the collection and stops the synchronization process of the collection.

On the other hand, local caching uses the dispose() method to clear the existing channel list. This method should be called when the user closes the channel list so that the SBDGroupChannelCollectionDelegate stops making changes to the channel list view.

Local cachingSyncManager
groupChannelCollection.dispose()

Objective-C

Copy link
Local cachingSyncManager
[self.groupChannelCollection dispose];