Local caching
Local caching is a better option than SyncManager for Android for rapid access to the data from the local cache and Sendbird server. If you are currently using SyncManager, we strongly encourage you to migrate to the Chat SDK with local caching for enhanced usability and easier maintenance.
Note: SyncManager will be deprecated soon.
The following table displays the full range of differences between local caching and SyncManager.
Local caching | SyncManager | |
---|---|---|
Implementation | Integrated to Chat SDK | Add-on to Chat SDK |
Architecture | View-driven | Event-driven |
Synchronization | ||
Supported | Not supported |
One distinct advantage of local caching over SyncManager is that it provides an event context
and a callback for identifying what changes are made to which group channels and messages. This facilitates locating and debugging issues in those channels.
SyncManager relies on an event-driven architecture where its collection handlers receive all events that pass through the WebSocket connection along with the data in the local cache. In contrast, local caching relies on a view-driven structure where only those events that directly affect the user’s channel list and chat view are delivered to their respective collections.
Another advantage of local caching is that messages that were not sent the first time are automatically resent at a later time. When the WebSocket connection is lost, messages considered “pending” are stored locally until the connection comes back on, at which point the pending messages are automatically sent. This new functionality unique to local caching is called auto resend.
Note: SyncManager will continue to work if the dependency to SyncManager is not removed. The dependency should be deleted once the migration to local caching is completed.
Initialization
You can initialize the Chat SDK with or without local caching. The useLocalCaching
is the boolean that determines if you are going to be using local caching. By default, the Chat SDK is initialized without local caching, the useLocalCaching
set to false. If the useLocalCaching
is set to true, the Chat SDK is initialized with local caching.
In local caching, the InitResultHandler()
is added to the SendBird.init()
method, which receives the initialization status through different event handlers. The onMigrationStarted()
is called when there is a change in the local cache. Meanwhile, the values of the onInitSucceeded()
and the onInitFailed()
let the client app know whether the Chat SDK has been successfully initialized.
Note: If initialization fails to complete and the
onInitFailed()
is called while theuseLocalCaching
is set to true, Chat SDK will operate as if theuseLocalCaching
is set to false.
Connect to Sendbird server
Local caching and SyncManager are alike in keeping chat in the client app up-to-date even when the app is not connected to Sendbird server. The key difference between SyncManager and local caching at the initialization stage is that local caching requires calling the SendBird.connect()
after the SendBird.init()
. Calling the SendBird.connect()
after the SendBird.init()
is optional when using SyncManager because the SyncManager.setup()
is called immediately after the SendBird.init()
during the initialization process. Calling the SendBird.connect()
after the SendBird.init()
is required when using local caching because some platforms receive the result of SendBird.init()
as a callback.
Other methods
Below you will find the new cache-related interfaces that come with local caching. Local caching’s clearCachedData()
replaces the functionality of SyncManager’s clearCache()
, which deletes the entire data file. In addition, the clearCachedMessages()
and the getCachedDataSize()
are available in local caching. The following table displays a full description of each method and if the method should be called before or after the SendBird.init()
.
Method | Description |
---|---|
| - Deletes the entire data file. |
| - Clears cached messages in a specific channel. |
| - Checks for the current cached data size. |