Migration from SyncManager
Local caching is available with Sendbird Chat SDK. Both local caching and SyncManager enables Sendbird Chat SDK for iOS to locally cache and retrieve data related to group channels and messages. However, local caching is integrated into Chat SDK while SyncManager is an external add-on. Being internally integrated into Chat SDK allows for easier maintenance, improved stability, and enhanced usability.
The existing SyncManager for iOS
Initialization
There are two ways to initialize the Chat SDK, with local caching or without local caching. During the initialization, the useCaching
determines whether the client app will use local caching with Sendbird Chat SDK or not. Its default value is false meaning the Chat SDK will be initialized without local caching.
In local caching, the completionHandler:
callbacks is added to the initWithApplicationId
method, which receives the initialization status through different event handlers. For Objective-C, the migrationStartHandler:
is called when there's an upgrade in the local cache. Meanwhile, the completionHandler:
informs the client app of whether the initialization is successful or not.
Note: If initialization fails to complete while the
useCaching
is set to true, Chat SDK will operate as if theuseCaching
is set to false.
Connect to Sendbird server
The difference between local caching and SyncManager in terms of the SBDMain.connect()
is whether it is necessary to call the SBDMain.connect()
during initialization or not. Both local caching and SyncManager ensure that your client app is operational during offline mode. However, to use the offline mode in SyncManager, calling the SBDMain.connect()
during the initialization process was optional. Instead, you only had to call the SBSMSyncManager.setup
after the initWithApplicationId
. In local caching, however, calling the SBDMain.connect()
after the initWithApplicationId
is required during the initialization process.
Swift
Objective-C
Group channel collection
A SBDGroupChannelCollection
allows you to swiftly create a channel list without missing any channel-related updates by retrieving group channels from both the local cache and Sendbird server. This page explains how to make a channel list using the collection and serves as a migration guide.
Create a collection
SyncManager's SBSMChannelCollection
is changed to the SBDGroupChannelCollection
in local caching. 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.
Swift
Objective-C
Channel events
In local caching, the SBDGroupChannelCollectionDelegate
is used to determine how the client app would react to channel-related events. SyncManager's SBSMChannelCollectionDelegate
, which is used to handle real-time events, should be changed as shown in the code below.
The following table shows when to call each event handler.
Event | Called when |
---|---|
| - A new group channel is created as a real-time event. |
| - The channel information that is included in the user's current chat view is updated as a real-time event. |
| - A group channel is deleted as a real-time event. |
Swift
Objective-C
List channels
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.
Methods | Description |
---|---|
| - Checks if there are more channels to load. |
| - If |
Swift
Objective-C
Dispose of the collection
SyncManager's SBSMChannelCollection
has the remove()
method which clears all the channels managed by the collection and stops synchronization process of the collection.
On the other hand, local caching uses the dispose()
method to clear the existing channel list view. This method should be called when the user closes the channel list so that the SBDGroupChannelCollectionDelegate
won't make any change to the channel list.
Swift
Objective-C
Message collection
A SBDMessageCollection
allows you to swiftly create a chat view without missing any message-related updates by retrieving data from both the local cache and Sendbird server. This page explains how to create a chat view using the collection and serves as a migration guide.
Create a collection
In local caching, the SBDMessageListParams
is used instead of SyncManager's SBSMMessageFilter
.
A SBDMessageListParams
instance will determine how to sort and order the retrieved messages. Then, specify the starting point of the message list in the chat view using the collection's builder.
Swift
Objective-C
Message events
Use the SBDMessageCollectionDelegate
to determine how the client app would react to message-related events.
A new addition to local caching is the didDetectHugeGap:
. If more than 300 messages are missing in the local cache compared to the remote server, Sendbird Chat SDK determines that there is a huge gap. For further information, see Gap and synchronization.
The following table shows when to call each event handler.
Delegate | Called when |
---|---|
| - A new message is created as a real-time event. |
| - A message is deleted as a real-time event. |
| - A message is updated as a real-time event. |
| - The channel information that is included in the user's current chat view is updated as a real-time event. |
| - The current channel is deleted as a rea-time event. |
| - A huge gap is detected when more than 300 messages are missing in the local cache compared to the remote server. In this case, you need to dispose of the view and create a new |
SyncManager's didReceive:succeededMessages:
,didReceive:pendingMessages:
, and didReceive:failedMessages:
should be changed as shown in the code below.
Swift
Objective-C
SyncManager's didReceiveNewMessage:
used to notify a new message that had arrived should also be changed with the code below.
Swift
Objective-C
List messages
SyncManager's fetchInDirection
method retrieves messages from the local cache and delivers them to the SBSMMessageCollectionDelegate
instance. In local caching, the SBDMessageCollection
can retrieve and display messages through four new interfaces, hasPrevious
, hasNext
, loadPrevious
, and loadNext
.
Unlike the SBDGroupChannelCollection
, pagination works in both directions for messages because messages can be shown in either chronological or reverse chronological order depending on how you set the value of startingPoint
.
Method | Description |
---|---|
| - Checks if there are more messages to load from the previous page. |
| - If |
| - Checks if there are more messages to load in the next page. |
| - If |
Policy
In a SBDMessageCollection
, the initialization is dictated by the SBDMessageCollectionInitPolicy
. The SBDMessageCollectionInitPolicy
determines how initialization deals with the message data retrieved from the local cache and API calls. Because we only support cacheAndReplaceByApi
at this time, an additional implementation is required to clear the messages in the local cache before adding the messages from the remote server. Messages will first be retrieved from the cached list using cacheResultHandler:
. Next, apiResultHandler:
calls the API result list which then replaces the cached message list with messages received from the API call.
Swift
Objective-C
SyncManager's resetViewpointTimestamp
is used to reset the current message collection to the specified time. To reset a viewpoint timestamp in local caching, it is suggested that you dispose the current message collection and create a new collection.
Swift
Objective-C
Send a message
In local caching, the result of sending a message is handled internally through the SBDMessageCollectionDelegate
. First, pending messages are delivered to local caching's messageCollection(_:context:channel:addedMessages:)
. Whether the message succeeds or fails in sending, the result will be delivered to the messageCollection(_:context:channel:updatedMessages:)
. Thus, the process of sending the callbacks from the sendUserMessage()
and the sendFileMessage()
to handleSendMessageResponse()
, as done in SyncManager, is no longer needed.
Note: Don't add the pending, succeeded or failed message objects of the
sendMessage()
callback to your message list data source. This can cause duplicate messages in the chat view.
Swift
Objective-C
Resend a failed message
In local caching, the result of resending a message is handled internally through the SBSMMessageCollectionDelegate
. First, pending messages are delivered to local caching's messageCollection(_:context:channel:updatedMessages:)
. Whether the message has succeeded or failed in sending, the result will be delivered to messageCollection(_:context:channel:updatedMessages:)
. Thus, the process of sending the callbacks from resendMessage()
to handleSendMessageResponse()
, as done in SyncManager, is no longer needed.
Note: Don't add the pending, succeeded or failed message objects of the
resendMessage()
callback to your message list data source. This can cause duplicate messages in the chat view.
Swift
Objective-C
Update a message
SyncManager had to recognize that a message was successfully sent. The delivery status had to be manually set through SyncManager's messageCollection?.updateMessage(updatedMessage)
. In local caching, however, updating messages is handled internally and delivered to the messageCollection(_:context:channel:updatedMessages:)
.
Swift
Objective-C
Delete a message
There are two cases in deleting a message:
Deleting a sent message.
In SyncManager, the process of delivering the result of message deletion through the deleteMessage()
was required. However, this process is not needed in local caching.
Deleting a failed message.
The process is the same for both SyncManager and local caching where the failed message object is deleted explicitly from the local cache. In SyncManager, failed messages are deleted through the deleteMessage()
. The same can be done through the removeFailedMessages()
in local caching.
Swift
Objective-C
Dispose of the collection
SyncManager's SBSMMessageCollection
has a remove()
method that clears all the messages managed by the collection and stops synchronization processes in the collection instance.
On the other hand, local caching uses the dispose()
method to clear the existing chat view. You should call this method when the current user leaves the channel or when a new collection has to be created due to a huge gap detected by the didDetectHugeGap:
.
Swift
Objective-C
Other methods
The following are new cache-related interfaces that come with local caching. Local caching's SBDMain.clearCachedData()
replaces the functionality of SyncManager's SBSMSyncManager.clearCache()
, which deletes the entire data file. In addition, the SBDMain.clearCachedMessages()
and the SBDMain.getCachedDataSize()
are available in local caching. The following table describes each method and when it should be called in regards to the initWithApplicationId
.
Method | Description |
---|---|
| - Deletes the entire data file. |
| - Clears cached messages in a specific channel. |
| - Checks for the current cached data size. |
Swift
Objective-C
In addition, SyncManager's messageCollection?.messageCount()
is replaced wih the messageCollection?.getSucceededMessages()
in local caching. It is used to count the number of succeeded messages in the collection.