With metadata and metacounter which consist of key-value items, you can store additional information to users and channels. This page explains how to manage user metadata, channel metadata, and channel metacounter.
Note : User metadata, channel metadata, and channel metacounter can each have up to 5 key-value items. The key’s length must be no larger than 128 characters. For user metadata and channel metadata, the value must be
String
and its length must be no larger than 190 characters. For channel metacounter, the value must beInt
.
You can store additional information to users such as phone number, email or a long description of a user with user metadata, which can be fetched or rendered into the UI. User metadata in [String: String]
can be stored into a User
object.
To store a user metadata into a User
object, add key-value items after creating a [String: String]
object, and pass it as an argument to a parameter when calling the createMetaData(_:completionHandler:)
method. You can put multiple key-value items in the dictionary.
The procedure to update a user metadata is the same as creating a user metadata. Values of existing keys will be updated and values of new keys will be added.
You can retrieve user metadata by reading the metadata
property of a User
object.
You can delete a user metadata as below.
You can store additional information to channels such as background color or a long description of a channel with channel metadata, which can be fetched or rendered into the UI. A channel metadata is [String: String]
and it can be stored into a OpenChannel
or a GroupChannel
object.
To store a channel metadata into a channel, add key-value items after creating a [String: String]
object, and pass it as an argument to a parameter when calling the createMetaData(_:completionHandler:)
method. You can put multiple key-value items in the dictionary.
The procedure to update a channel metadata is the same as creating a channel metadata. Values of existing keys will be updated and values of new keys will be added.
You can retrieve channel metadata by creating a Array
object 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
with corresponding key-value items.
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 metadata through the getCachedMetaData()
method without having to query the server.
You can delete a channel metadata as below.
You can store additional information to channels such as the tracking number of Like
s with channel metacounter, which can be fetched or rendered into the UI. Channel metacounter in [String: Int]
can be stored into a OpenChannel
or a GroupChannel
object.
Note : A channel metacounter is primarily used to track and update discrete indicators in a channel. Use channel metacounter instead of channel metadata when you need an integer with atomic increasing and decreasing operations.
To store a metacounter into a channel, add key-value items after creating a [String: Int]
object, and pass it as an argument to a parameter when calling the createMetaCounters(_:completionHandler:)
method. You can put multiple key-value items in the dictionary.
The procedure to update a channel metacounter is the same as creating channel metacounter. Values of existing keys will be updated and values of new keys will be added.
You can increase values in a channel metacounter by passing a [String: Int]
of the items to increase as an argument to a parameter in the increaseMetaCounters(_:completionHandler:)
method. The values of corresponding keys in the metacounter will be incremented by the number you’ve specified.
You can decrease values in a channel metacounter by passing a [String: Int]
of the items to decrease as an argument to a parameter in the decreaseMetaCounters(_:completionHandler:)
method. The values of corresponding keys in the metacounter will be decremented by the number you’ve specified.
You can retrieve channel metacounter by creating a Array
of keys to retrieve and passing it as an argument to a parameter in the getMetaCounters(keys:completionHandler:)
method. Then a matching [String: Int]
value will return through completionHandler
with corresponding key-value items.
The process for deleting a metacounter is as simple as creating one.