Chat SDKs iOS v3
Chat SDKs iOS
Chat SDKs
Version 3
Sendbird Chat SDK v3 for iOS is no longer supported as a new version is released. Check out our latest Chat SDK v4

Caching data

Copy link

Storing chat data locally within devices helps users retrieve their channels and messages even while offline. It can also prevent the inefficiency of repeating queries upon each connection or when device state changes, as well as provide a desirable user experience by reducing data loading delays.

In this File caching: Basic page and Database caching: Advanced page, you can see how to build a local cache by using object serialization and deserialization, which is provided through the Chat SDK. In the page, we provide instructions on how to build a simple cache that stores the most recent messages and channels in a file. In the Database caching: Advanced page, you can find instructions on caching chat data in an internal database, which enables you to store structured and queryable data.

Caching with Sendbird SyncManager

Copy link

Sendbird SyncManager is a library for chat data synchronization, which allows you to cache channels and messages of those channels in your client app reliably with a little effort. It listens to the events which happen on the channels in real-time and executes the background tasks for updating the cached data accordingly. You can use the cached data for a better user experience purposes, such as retrieving old messages in a channel view without requesting to Sendbird server.

If you want a simple and fast caching implementation, download the SyncManager from our repository on GitHub and see the guide on how to add to your client app.

Note: The SyncManager includes SQLite as an internal database and operates based on it, which saves and stores the chat data synced with Sendbird server for caching.

File caching: Basic

Copy link

This page shows you how to build a simple cache that stores a user's most recent channels and messages. A cache can be used to load data when an offline user attempts to view a list of channels, or enters a channel to view their chat history. Implementing just a basic cache like the following can improve user experience, as users get to see empty lists of channels or messages due to their unstable connection.

In the steps described below, we create a file per channel in your client app's cache directory, write serialized data into the file to store a set amount of recent messages, configure the app to first load messages from the cache, and then finally replace them when the newest results are successfully fetched from Sendbird server.

Serialize and deserialize Sendbird objects

Copy link

In order to enable you to store Sendbird objects such as messages, channels, and users in a local storage, we provide serialization and deserialization methods through our Chat SDK.

By using the serialize method to convert a Sendbird object to binary data like the following, you can store the binary data natively in a file.

// In SBDBaseMessage.h
- (nullable NSData *)serialize;
+ (nullable instancetype)buildFromSerializedData:(NSData * _Nonnull)data;

Save serialized messages

Copy link

With serialization, you can store a channel and its most recent messages in a file. In this case, we are encoding the binary serialized data into a Base64 string. then storing each item in a new line. Normally, save data when onStop() is called in your user's chat screen.

Note: To keep your local database in sync with Sendbird server's data, your app should regularly check for changes made to messages in the server and apply those changes to the local cache accordingly. Because the getMessageChangeLogsWithToken:completionHandler: and getMessageChangeLogsByTimestamp:completionHandler: methods are deprecated as of August 2021, you can manage local database updates by retrieving change logs of messages using the getMessageChangeLogsSinceToken:params:completionHandler: or getMessageChangeLogsSinceTimestamp:params:completionHandler: methods.

// Saving a serialized message.
func save(message: SBDBaseMessage?) {
    guard let theMessage: SBDBaseMessage = message, theMessage.messageId != 0 else {    // Check the message.
        return      // A value of 0 indicates that the message has not sent to Sendbird server yet. The message of which ID is 0 should be excluded from caching

    // Serialization
    let messageData: Data? = theMessage.serialize()

    // Encryption
    let messageHash: String? = SHA256StringFromData(messageData)    // Implement your own function for SHA256 encryption.

    // Writing the hashed message to a local file
    SaveHashIntoFile(messageHash)       // Implement your own function which saves a hashed message into a local file.

Note: In this case, SHA256 hashing is used to generate a hash file for each stored data file. Using this hash file, you can check if the newly generated data differs from the corresponding one already stored in the cache, preventing an unnecessary overwriting.

Load messages with deserialization

Copy link

When your user enters a chat to view their message history, load saved messages from the cache.

// Loading messages with deserialization.
func loadMessages(channelUrl: String) -> [SBDBaseMessage] {
    // Load hashed messages from a local file
    let messageHashs: String? = LoadHashFromFile(channelUrl)

    // Decryption of the hashed messages
    let messageData: [Data]? = DecryptStringBySHA256(messageHashs?)     // Implement your own function for SHA256 decryption.

    // Deserialization
    var messages: [SBDBaseMessage] = []
    for data in messageData {
        let message: SBDBaseMessage? = data)
        if let theMessage: SBDBaseMessage = message {

    return messages

After receiving an updated message list from Sendbird server, clear the current message list and replace it with the updated list. In effect, messages from the cache are overwritten almost instantly if the user's connection is normal.

Save and load channels

Copy link

We recommend that you only cache group channels in a local file rather than open channels. The property values and participant status of open channels can be frequently changed depending on your use case, and you might go through difficulty in syncing the local file with the changes.

The process of caching channels is similar to caching messages. For the sake of brevity, the following code is provided without additional explanations.

Note: In group channels, information associated with them can be updated or the current user might leave them anytime. To keep your cache synced with data in Sendbird server, your client app should check information about changes to the channels regularly and apply the changes to the cache. You can retrieve change logs of the current user's group channels by using getMyGroupChannelChangeLogsByToken:customTypes:completionHandler: or getMyGroupChannelChangeLogsByTimestamp:customTypes:completionHandler: method, with which you can manage cache updates.

// Saving serialized group channels.
func save(channels: [SBDGroupChannel]?) {
    guard let theChannels: [SBDGroupChannel] = channels else {

    var hashs: [String] = []
    for channel in theChannels {
        // Serialization
        let data: Data? = channel.serialize()

        // Encryption
        let hash: String? = SHA256StringFromData(data)      // Implement your own function for SHA256 encryption.

        if let theHash: String = hash {

    // Writing the hashed channels into a local file.
    SaveHashIntoFile(hashs)         // Implement your own function which saves hashed channels into a local file.

// Loading group channels with deserialization.
func loadChannels() -> [SBDGroupChannel] {
    // Load the hashed channels from from a local file.
    let hashs: [String] = LoadChannelHashsFromFile()

    var channels: [SBDGroupChannel] = []
    for hash in hashs {
        // Decryption
        let data: Data? = DecryptStringBySHA256(hash)

        // Deserialization
        let channel: SBDGroupChannel? = data)
        if let theChannel: SBDGroupChannel = channel {

    return channels