Channel sync
This page provides an overview of how channels can be synchronized by using the ChannelCollection
together with other components. The ChannelCollection
is a view adapter that subscribes to channel-related events through the ChannelCollectionHandler
. As the handler 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 handler. At the same time, cached events are also delivered to the handler. 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 handler.
Initialization
The following shows how to initialize and use a ChannelCollection
instance:
In the above sample, the collection sets the collection handler to listen to channel events and call the fetch()
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
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 ChannelCollectionHandler
's onChannelEvent()
callback method:
Real-time channel events
Event | Action with description |
---|---|
onChannelChanged |
|
onChannelHidden |
|
onChannelDeleted |
|
onChannelFrozen |
|
onChannelUnfrozen |
|
onMetaDataCreated |
|
onMetaDataUpdated |
|
onMetaDataDeleted |
|
onMetaCounterCreated |
|
onMetaCounterUpdated |
|
onMetaCounterDeleted |
|
onUserReceivedInvitation |
|
onUserDeclinedInvitation |
|
onUserJoined |
|
onUserLeft |
|
onReadReceiptUpdated |
|
onUserMuted |
|
onUserUnmuted |
|
onUserBanned |
|
onUserUnbanned |
|
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()
The fetch()
method fetches channels from the local cache and delivers them to the ChannelCollectionHandler
instance. 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
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 MyGroupChannelListQuery
's order
option from the Chat SDK, cached channels can also be listed chronologically as well as alphabetically by channel name or metadata value:
Close the view
When the channel list view is closed, the collection should be cleared. A ChannelCollection
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.