/ SDKs / Android
SDKs
Chat SDKs Android v4
Chat SDKs Android
Chat SDKs
Android
Version 4

Manage channel metacounters

Copy link

Using a channel metacounter, you can store additional information to channels such as the tracking the number of likes on a message. A channel metacounter in Map<String, Integer> can be stored into a Channel object and can be fetched or rendered into the UI.

A channel metacounter is primarily used to track and update discrete indicators in a channel. Use channel metacounters instead of channel metadata when you need an integer with increasing and decreasing atomic operations.


Create a metacounter

Copy link

To store channel metacounter into a Channel object, create Map<String, Integer> and add key-value items. Then pass the map as an argument to a parameter when calling the createMetaCounters() method. You can put multiple key-value items in the map.

KotlinKTX
val counters = mapOf("key1" to 1, "key2" to 2)
channel.createMetaCounters(counters) { map, e ->
    if (e != null) {
        // Handle error.
    }

    // ...
}

Update a metacounter

Copy link

The procedure of updating a channel metacounter is the same as creating a channel metacounter. Values of existing keys are updated and values of new keys are added.

KotlinKTX
val counters = mapOf(
    "key1" to 3,    // Update 1 to 3.
    "key2" to 4,    // Update 2 to 4.
    "key3" to 0     // Add a new key-value item.
)
channel.updateMetaCounters(counters) { map, e ->
    if (e != null) {
        // Handle error.
    }

    // ...
}

Increase a metacounter

Copy link

You can increase values in a channel metacounter by passing a Map of keys to increase as an argument to a parameter in the increaseMetaCounters() method. method. The values of corresponding keys in the metacounter are increased by the specified number.

KotlinKTX
val counters = mapOf(
    "key1" to 2,    // Increase by 2.
    "key2" to 3     // Increase by 3.
)
channel.increaseMetaCounters(counters) { map, e ->
    if (e != null) {
        // Handle error.
    }

    // ...
}

Decrease a metacounter

Copy link

You can decrease values in a channel metacounter by passing a Map of keys to decrease as an argument to a parameter in the decreaseMetaCounters() method. The values of corresponding keys in the metacounter are decreased by the specified number.

KotlinKTX
val counters = mapOf(
    "key1" to 3,    // Decrease by 3.
    "key2" to 4     // Decrease by 4.
)
channel.decreaseMetaCounters(counters) { map, e ->
    if (e != null) {
        // Handle error.
    }

    // ...
}

Retrieve a metacounter

Copy link

You can retrieve a channel metacounter by creating a List of keys to retrieve and passing it as an argument to a parameter in the getMetaCounters() method. A Map<String, Integer> collection is returned through a callback handler with the corresponding key-value items.

KotlinKTX
val keys = listOf("key1", "key2")
channel.getMetaCounters(keys) { map, e ->
    if (e != null) {
        // Handle error.
    }

    // ...
}

Delete a metacounter

Copy link

You can delete a channel metacounter as shown below.

KotlinKTX
channel.deleteMetaCounter("key1") { e ->
    if (e != null) {
        // Handle error.
    }

    // ...
}