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

Channel sync

Copy link

This page provides an overview of how channels can be synchronized by using the SBSMChannelCollection together with other classes. The SBSMChannelCollection is a class that subscribes to channel-related events through the SBSMChannelCollectionDelegate. Because the delegate listens to both cached events and real-time events, it can be leveraged to direct view implementation.

For example, if SyncManager receives a real-time event that the current user left a channel, it creates a collection event which has the remove action for the channel and delivers it to the collection delegate. At the same time, cached events are also delivered to the delegate. The collection uses the fetch method to load channels from the cache and then delivers a collection event, which has the insert action for the channels, to the collection delegate.


Initialization

Copy link

The following shows how to initialize and use a SBSMChannelCollection instance:

Objective-CSwift
SBDGroupChannelListQuery *listQuery = [SBDGroupChannel createMyGroupChannelListQuery];
SBSMChannelCollection *collection = [SBSMChannelCollection collectionWithQuery:listQuery];
collection.delegate = self;
[collection fetchWithCompletionHandler:^(SBDError *error) {}];

// Channel collection delegate
- (void)collection:(SBSMChannelCollection *)collection didReceiveEvent:(SBSMChannelEventAction)action channels:(NSArray<SBDGroupChannel *> *)channels {
    switch (action) {
        case SBSMChannelEventActionClear: {
            // Clear the view.
            break;
        }
        case SBSMChannelEventActionInsert: {
            // Add channels to the view.
            break;
        }
        case SBSMChannelEventActionUpdate: {
            // Update channels in the view.
            break;
        }
        case SBSMChannelEventActionRemove: {
            // Remove channels from the view.
            break;
        }
        case SBSMChannelEventActionMove: {
            // Change the position of channels in the view.
            break;
        }
        case SBSMChannelEventActionNone:
        default: {
            break;
        }
    }
}

In the above sample, the collection sets the collection delegate to listen to channel events and call the fetch method after initialization. At runtime, according to your caching status, the insert event can return once or twice. The first event is called to fetch channels from the cache. If not enough channels are cached, SyncManager waits until background sync synchronizes with the server before delivering the rest of the channels. Then, you may get more events with those channels.


Real-time events

Copy link

SyncManager listens to real-time events from the Chat SDK in order to apply changes and notify the current user. Below is the list of real-time channel events that SyncManager listens to through the SBSMChannelCollectionDelegate's didReceiveEvent: callback method:

Real-time channel events

Copy link
EventAction with description

channelWasChanged:

SBSMChannelEventActionUpdate or SBSMChannelEventActionInsert: One of the following group channel properties has been changed: distinct, push notification preferences, last message (excluding silent admin messages), unread message count, name, cover image, data, or custom type. If the collection doesn't have the channel in the channel list, an insert event is given instead of update. If the property change affects the order of the channel list, the move event is also invoked.

channelWasHidden:

SBSMChannelEventActionUpdate: A group channel has been hidden from the list. If the collection doesn't have the channel in the channel list, the event is ignored.

channelWasDeleted:

SBSMChannelEventActionRemove: A group channel has been deleted. If the collection doesn't have the channel in the channel list, the event is ignored.

channelWasFrozen:

SBSMChannelEventActionUpdate: A group channel has been frozen. If the collection doesn't have the channel in the channel list, the event is ignored.

channelWasUnfrozen:

SBSMChannelEventActionUpdate: A group channel has been unfrozen. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:createdMetaData:

SBSMChannelEventActionUpdate: A metadata for a group channel has been created. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:updatedMetaData:

SBSMChannelEventActionUpdate: A metadata for a group channel has been updated. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:deletedMetaData:

SBSMChannelEventActionUpdate: A metadata for a group channel has been deleted. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:createdMetaCounters:

SBSMChannelEventActionUpdate: A metacounter for a group channel has been created. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:updatedMetaCounters:

SBSMChannelEventActionUpdate: A metacounter for a group channel has been updated. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:deletedMetaCounters:

SBSMChannelEventActionUpdate: A metacounter for a group channel has been deleted. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:didReceiveInvitation:

SBSMChannelEventActionUpdate: A user has been invited to a group channel. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:didDeclineInvitation:

SBSMChannelEventActionUpdate or SBSMChannelEventActionRemove: A user has declined an invitation to a group channel. If the user is the current user, a remove event is given instead of update. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:userDidJoin:

SBSMChannelEventActionUpdate: A user has joined a group channel. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:userDidLeave:

SBSMChannelEventActionUpdate or SBSMChannelEventActionRemove: A user has left a group channel. If the user is the current user, a remove event is given instead of update. If the collection doesn't have the channel in the channel list, the event is ignored.

channelDidUpdateReadReceipt:

SBSMChannelEventActionUpdate: A user has read a specific unread message in a group channel. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:userWasMuted:

SBSMChannelEventActionUpdate: A user has been muted in a group channel. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:userWasUnmuted:

SBSMChannelEventActionUpdate: A user has been unmuted in a group channel. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:userWasBanned:

SBSMChannelEventActionUpdate or SBSMChannelEventActionRemove: A user has been banned from a group channel. If the user is the current user, a remove event is given instead of update. If the collection doesn't have the channel in the channel list, the event is ignored.

channel:userWasUnbanned:

SBSMChannelEventActionUpdate: A user has been unbanned from a group channel. If the collection doesn't have the channel in the channel list, the event is ignored.

Note: The collection has an array that holds all the channels that should be shown in the view. If a channel isn’t shown in the view, it means that the collection isn’t holding it because background sync synchronization hasn’t been completed at that time.


fetch method

Copy link

The fetch method fetches channels from the local cache and delivers them to the delegate. If not enough channels are cached, the fetch waits until background sync synchronizes and brings the retrieved channels. The fetch should be called when:

  • A collection is created.
  • A connection is established.
  • The app goes to the foreground.
  • The scroll reaches the bottom.
  • The app returns from a locked screen when still in the foreground.

The fetch subscribes to background sync to pull data from the local cache. As the fetch doesn’t directly interact with Sendbird server, while online, it waits until background sync synchronizes the server to the cache before retrieving new data. However, after synchronization is completed by background sync, the fetch can refresh the view with new data. Therefore, the fetch can not only read cached data, but also update data in the view.


Channel order

Copy link

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

Note: When a message arrives in an uncached channel, the channel is automatically cached and brought to the top of the list since it most recently received a message.

If needed, by changing the value of the SBDGroupChannelListQuery's order option from the Chat SDK, cached channels can also be listed chronologically as well as alphabetically by channel name or metadata value:

Objective-CSwift
SBDGroupChannelListQuery *listQuery = [SBDGroupChannel createMyGroupChannelListQuery];
listQuery.includeEmptyChannel = YES;
listQuery.order = SBDGroupChannelListOrderChannelNameAlphabetical;  // SBDGroupChannelListOrderChronological, SBDGroupChannelListOrderLatestLastMessage, SBDGroupChannelListOrderChannelNameAlphabetical, and SBDGroupChannelListOrderChannelMetaDataValueAlphabetical
...

Close the view

Copy link

When the channel list view is closed, the collection should be cleared. A SBSMChannelCollection has the remove method which clears all the channels managed by the collection and stops synchronization processes in the collection. If the collection isn’t explicitly dropped, the applicable memory won’t be released and could lead to performance slowdown.