Chat SDKs iOS v3
Chat SDKs iOS
Chat SDKs
iOS
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

Database caching: Advanced

Copy link

This page shows you how to build a local cache using a database. This has several advantages like storing raw data in a file and enabling particular queries on stored channels and messages. The content is provided on the assumption that you use SQLite database as a local storage, you can easily follow these steps with any other database like Realm.


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 save the binary data natively in your persistent database.

SwiftObjective-C
// In SBDBaseMessage.h
func serialize() -> Data?
class func build(fromSerializedData data: Data) -> Self?

Save and load messages with serialization and deserialization

Copy link

Your database should have a table to save and load messages, which has columns corresponding to the properties of a message object.

Design a message table

Copy link

A basic table to store messages contains the following columns:

message_idchannel_urlmessage_tspayload

22312323345

sendbird_channel_2348023

1432039402493

Serialized data

23623445361

sendbird_channel_2348023

1432039403243

Serialized data

Caching procedures

Copy link
  1. By using the getMessagesByTimestamp:params:completionHandler: method, you can fetch new messages in a channel. With the returned messages, serialize and insert each message into your database. However, we recommend storing the message ID, timestamp, and channel URL in separate columns using message.messageId, message.createdAt, and message.channelUrl. This allows you to query the data on a row-by-row basis later on.
  2. Before loading messages within a channel, sort rows in the chronological order by message_ts. Then, deserialize each message and display them in your UI.
  3. When loading previous messages that are not currently stored in the local database, obtain the timestamp of the earliest stored message. Then, query for messages created before that value.
  4. Likewise, when loading new messages, query for messages with a later timestamp than the most recent message.

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.

SwiftObjective-C
// Example 1: When joining a channel

var dbpath: String = "<DATABASE_PATH>"
var contactDB: OpaquePointer? = nil
var statement: OpaquePointer? = nil

// Loading messages from your local database.
if sqlite3_open(dbpath, &contactDB) == SQLITE_OK {
    let query: String = "SELECT * FROM (SELECT * FROM MESSAGE WHERE COLUMN_NAME_CHANNEL_URL = ? AND COLUMN_NAME_TIMESTAMP < ? ORDER BY COLUMN_NAME_TIMESTAMP DESC LIMIT ?) ORDER BY COLUMN_NAME_TIMESTAMP ASC"

    if sqlite3_prepare_v2(contactDB, query, -1, &statement, nil) == SQLITE_OK {
        sqlite3_bind_text(statement, 1, "<CHANNEL_URL>".UTF8String, -1, SQLITE_TRANSIENT) // COLUMN_NAME_CHANNEL_URL
        sqlite3_bind_int64(statement, 2, timestamp) // COLUMN_NAME_TIMESTAMP
        sqlite3_bind_int64(statement, 3, limit)     // COLUMN_NAME_TIMESTAMP

        // Create a list of 'SBDBaseMessage' objects by deserializing each.
        var messageList: [SBDBaseMessage] = []
        while (sqlite3_step(statement) == SQLITE_ROW) {
            guard let payload = sqlite3_column_blob(statement, 4) else { return }
            let size = sqlite3_column_bytes(statement, 4)
            let data: Data = Data.init(bytes: payload, count: Int(size))
            let message: SBDBaseMessage? = SBDBaseMessage.build(fromSerializedData: data)
            if let theMessage: SBDBaseMessage = message {
                messageList.append(theMessage)
            }
        }

        sqlite3_finalize(statement)

        // Pass messages to data source for displaying them in a UITableView, UICollectionView, etc.
        self.messages.append(messageList)

        // Get new messages from Sendbird server
        let latestStoredTimestamp: Int64  = messageList.first?.createdAt ?? LONG_LONG_MAX // Get the timestamp of the last stored message.
        let params = SBDMessageListParams()
        params.previousResultSize = 30
        params.reverse = false

        channel.getMessagesByTimestamp(latestStoredTimestamp, params: params) { (messages, error) in
            guard let _: Error = error else {
                // Handle error.
            }

            // New messages successfully fetched.
            self.messages.append(messages)

            // Insert each new message into your local database.
            let query: String = "INSERT INTO MESSAGE (message_id, channel_url, message_ts, payload) VALUES (?, ?, ?, ?)"

            for message in messages ?? [SBDBaseMessage] {
                // Store each new message in the local database.
                if (sqlite3_prepare_v2(contactDB, query, -1, &statement, nil) == SQLITE_OK) {
                    sqlite3_bind_int64(statement, 1, message.messageId) // message_id
                    sqlite3_bind_text(statement, 2, message.channelUrl.UTF8String, -1, SQLITE_TRANSIENT) // channel_url
                    sqlite3_bind_int64(statement, 3, message.createdAt) // message_ts

                    let blob: Data? = message.serialize()
                    sqlite3_bind_blob(statement, 4, blob?.bytes, blob?.length, SQLITE_TRANSIENT)
                }

                if (sqlite3_step(statement) != SQLITE_DONE) {
                    // Handle error.
                }

                sqlite3_finalize(statement)
            }
        }
    }

    sqlite3_finalize(statement)
}

sqlite3_close(contactDB)
SwiftObjective-C
// Example 2: When receiving a new message

func channel(_ sender: SBDBaseChannel, didReceive message: SBDBaseMessage) {
    guard let channel: SBDGroupChannel = sender else {
        return
    }

    // Pass the message to your data source for UITableView or UICollectionView.
    self.messages.append(message)

    // Store the message in your local database.
    // It is a good idea to have a helper class or method for database transactions.
    var dbpath: String = "<DATABASE_PATH>"
    var contactDB: OpaquePointer? = nil
    var statement: OpaquePointer? = nil

    if sqlite3_open(dbpath, &contactDB) == SQLITE_OK {
        // Insert each new message into your local database.
        let query: String = "INSERT INTO MESSAGE (message_id, channel_url, message_ts, payload) VALUES (?, ?, ?, ?)"

        // Store each new message in the local database.
        if sqlite3_prepare_v2(contactDB, query, -1, &statement, nil) == SQLITE_OK {
            sqlite3_bind_int64(statement, 1, message.messageId) // message_id
            sqlite3_bind_text(statement, 2, message.channelUrl.UTF8String, -1, SQLITE_TRANSIENT) // channel_url
            sqlite3_bind_int64(statement, 3, message.createdAt) // message_ts

            let blob: Data? = message.serialize()
            sqlite3_bind_blob(statement, 4, blob?.bytes, blob?.length, SQLITE_TRANSIENT)
        }

        if (sqlite3_step(statement) != SQLITE_DONE) {
            // Handle error.
        }

        sqlite3_finalize(statement)
    }

    sqlite3_close(contactDB)
}
SwiftObjective-C
// Example 3: When sending a message

channel.sendUserMessage(text, completionHandler: { (userMessage, error) in
    guard let message: SBDUserMessage = userMessage, error == nil else {
        return
    }

    // Pass the message to your data source for UITableView or UICollectionView.
    self.messages.append(message)

    // Store the message in your local database.
    // It is a good idea to have a helper class or method for database transactions.
    var dbpath: String = "<DATABASE_PATH>"
    var contactDB: OpaquePointer? = nil
    var statement: OpaquePointer? = nil

    if sqlite3_open(dbpath, &contactDB) == SQLITE_OK {
        // Insert each new message into your local database.
        let query: String = "INSERT INTO MESSAGE (message_id, channel_url, message_ts, payload) VALUES (?, ?, ?, ?)"

        // Store each new message in the local database.
        if sqlite3_prepare_v2(contactDB, query, -1, &statement, nil) == SQLITE_OK {
            sqlite3_bind_int64(statement, 1, message.messageId) // message_id
            sqlite3_bind_text(statement, 2, message.channelUrl.UTF8String, -1, SQLITE_TRANSIENT) // channel_url
            sqlite3_bind_int64(statement, 3, message.createdAt) // message_ts

            let blob: Data? = message.serialize()
            sqlite3_bind_blob(statement, 4, blob?.bytes, blob?.length, SQLITE_TRANSIENT)
        }

        if (sqlite3_step(statement) != SQLITE_DONE) {
            // Handle error.
        }

        sqlite3_finalize(statement)
    }
})

Caveats

Copy link

Currently, it is difficult to sync deleted or edited messages. We are working to provide this feature in both our Chat SDKs and APIs, and hope to release it soon.


Save and load channels with serialization and deserialization

Copy link

Your database should have a table to save and load channels, which has columns corresponding to the properties of a channel object.

Note: The following examples are based on a group channel. To cache an open channel, slightly improvise from the directions below (such as changing last_message_ts to channel_created_at).

Design a channel table

Copy link

A basic table to store channels contains the following columns:

channel_urllast_message_tspayload

sendbird_channel_2348023

1432039402729

Serialized data

sendbird_channel_2348023

1432039403448

Serialized data

Caching procedures

Copy link
  1. After fetching new channels by using a SBDOpenChannelListQuery or SBDGroupChannelListQuery, serialize and insert each channel into your database. We recommend storing the channel URL and timestamp of the last message in separate columns by using channel.channelUrl and channel.lastMessage.createdAt. This allows you to query the data on a row-by-row basis later on.
  2. Before loading a list of channels, sort rows in the chronological order by last_message_ts. Then, deserialize each channel and display them in your UI.
  3. Unlike messages, channels are relatively few in number and go through frequent property changes, such as cover URL and name changes, or their deletions. We recommend updating a cache by completely replacing with new data when possible.
  4. When real-time changes are made to a channel list, update your cache.

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

SwiftObjective-C
// Example 1: When displaying a list of channels

var dbpath: String = "<DATABASE_PATH>"
var contactDB: OpaquePointer? = nil
var statement: OpaquePointer? = nil

// Loading channels from your local database.
if sqlite3_open(dbpath, &contactDB) == SQLITE_OK {
    let query: String = "SELECT * FROM CHANNEL ORDER BY COLUMN_NAME_LAST_MESSAGE_TIMESTAMP ASC"

    if sqlite3_prepare_v2(contactDB, query, -1, &statement, nil) == SQLITE_OK {
        // Create a list of `SBDBaseChannel` objects by deserializing each.
        var channels: [SBDGroupChannel] = []
        while (sqlite3_step(statement) == SQLITE_ROW) {
            guard let payload = sqlite3_column_blob(statement, {iCol}) else { return }
            let size = sqlite3_column_bytes(statement, {iCol})
            let data: Data = Data.init(bytes: payload, count: Int(size))
            let channel: SBDGroupChannel? = SBDGroupChannel.build(fromSerializedData: data)
            if let theChannel: SBDGroupChannel = channel {
                channels.append(theChannel)
            }
        }
        sqlite3_finalize(statement)

        sqlite3_close(contactDB)

        // Pass messages to data source for displaying them in a UITableView, UICollectionView, etc.
        self.channels.append(channels)

        // Get new channels from Sendbird server.
        let listQuery: SBDGroupChannelListQuery? = SBDGroupChannel.createMyGroupChannelListQuery()
        listQuery?.loadNextPage(completionHandler: { (channels, error) in
            guard error == nil else {
                // Handle error.
            }

            // Replace the current (cached) data set.
            self.channels.removeAll()
            self.channels.append(channels)

            // Save channels.
            SaveChannelsToDatabase(channels)
        })
    }

    sqlite3_finalize(statement)
}

sqlite3_close(contactDB)
SwiftObjective-C
// Example 2: On real-time events such as adding or updating a channel

func channelWasChanged(_ sender: SBDBaseChannel) {
    guard let channel: SBDGroupChannel = sender as? SBDGroupChannel else {
        return
    }

    var dbpath: String = "<DATABASE_PATH>"
    var contactDB: OpaquePointer? = nil
    var statement: OpaquePointer? = nil

    if sqlite3_open(dbpath, &contactDB) == SQLITE_OK {
        let query: String = "SELECT * FROM CHANNEL WHERE channel_url = ?"

        if sqlite3_prepare_v2(contactDB, query, -1, &statement, nil) == SQLITE_OK {
            sqlite3_bind_text(statement, 1, channel.channelUrl.UTF8String, -1, SQLITE_TRANSIENT) // channel_url
        }

        if sqlite3_step(statement) == SQLITE_ROW {
            // If the channel is not currently cached, add it.
            sqlite3_finalize(statement)

            let query: String = "INSERT INTO CHANNEL (channel_url, last_message_ts, payload) VALUES (?, ?, ?)"

            // Store new channel in the local database.
            if sqlite3_prepare_v2(contactDB, query, -1, &statement, nil) == SQLITE_OK {
                sqlite3_bind_text(statement, 1, channel.channelUrl.UTF8String, -1, SQLITE_TRANSIENT) // channel_url
                sqlite3_bind_int64(statement, 2, channel.lastMessage?.createdAt ?? 0) // last_message_ts

                let blob: Data? = channel.serialize()
                sqlite3_bind_blob(statement, 3, blob?.bytes, blob?.length, SQLITE_TRANSIENT)
            }

            if (sqlite3_step(statement) != SQLITE_DONE) {
                // Handle error.
            }

            sqlite3_finalize(statement)
        } else {
            // If the channel is in the current cache, update it.
            sqlite3_finalize(statement)

            let query: String = "UPDATE CHANNEL SET last_message_ts = ?, payload = ? WHERE channel_url = ?"
            if sqlite3_prepare_v2(contactDB, query, -1, &statement, nil) == SQLITE_OK {
                sqlite3_bind_int64(statement, 1, channel.lastMessage?.createdAt ?? 0) // last_message_ts

                let blob: Data? = channel.serialize()
                sqlite3_bind_blob(statement, 2, blob?.bytes, blob?.length, SQLITE_TRANSIENT)
                sqlite3_bind_text(statement, 3, channel.channelUrl.UTF8String, -1, SQLITE_TRANSIENT) // channel_url

                if (sqlite3_step(statement) != SQLITE_DONE) {
                    // Handle error.
                }

                sqlite3_finalize(statement)
            }
        }
    }

    sqlite3_close(contactDB)
}

Note: A similar process can be applied to the channelWasDeleted:channelType:, channel:userDidJoin:, and channel:userDidLeave: method.