/ SDKs / iOS
SDKs
Chat SDKs iOS v4
Chat SDKs iOS
Chat SDKs
iOS
Version 4

Manage channel metadata

Copy link

You can store additional information to channels such as background color or channel description with channel metadata, which can be fetched or rendered into the UI. Channel metadata is [String: String] and it can be stored into an OpenChannel object or a GroupChannel object.

To receive and retrieve information about events happening in channels from the Sendbird server, you can add a channel delegate. For more information on channel delegates, see Channel event types.


Create metadata

Copy link

To store channel metadata into a Channel object, create a newMetaData object of key-value items in which the data type of the key and value is String. Then, pass the object as an argument to a parameter when calling the createMetaData(_:completionHandler:) method. You can put multiple key-value items in the dictionary.

let newMetaData = [
    "key1": "value1",
    "key2": "value2"
]

channel.createMetaData(newMetaData) { metaData, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

Update metadata

Copy link

The process of updating channel metadata is the same as creating one. Values of existing keys can be updated and values of new keys can be added by calling the updateMetaData()method.

let metaDataToUpdate = [
    "key1": "valueToUpdate1",   // Update an existing item with a new value.
    "key2": "valueToUpdate2",   // Update an existing item with a new value.
    "key3": "valueToAdd3"       // Add a new key-value item.
]

channel.updateMetaData(metaDataToUpdate) { metaData, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

Retrieve metadata

Copy link

You can retrieve channel metadata by creating an Array of keys to retrieve and passing it as an argument to a parameter in the getMetaData(keys:completionHandler:) method. A [String: String] object will return through the completionHandler callback function with corresponding key-value items.

let keys = ["key1", "key2"]

channel.getMetaData(keys: keys) { metaData, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

Retrieve cached metadata

Copy link

When Sendbird Chat SDK detects any of the create, read, update, and delete operations on the channel metadata, the SDK caches the metadata. The cached metadata is also updated whenever a channel list is fetched.

You can retrieve the cached metadata through the getCachedMetaData() method without having to query the server.

let cachedMetaData = channel.getCachedMetaData()
let value = cachedMetaData[key] // The key should be string.

Delete metadata

Copy link

You can delete channel metadata by calling the deleteMetaData() method.

channel.deleteMetaData(key: "key1") { error in
    guard error == nil else {
        // Handle error.
        return
    }
}