/ SDKs / Unity
SDKs
Chat SDKs Unity v4
Chat SDKs Unity
Chat SDKs
Unity
Version 4

Manage channel metacounters

Copy link

Using a channel metacounter, you can store additional information to channels such as the number of likes on a message. A channel metacounter in Dictionary<string, int> 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, create Dictionary<string, int> and add key-value items. Then pass the dictionary as an argument to a parameter when calling the CreateMetaCounters() method. You can put multiple key-value items in the dictionary.

Dictionary<string, int> metaCounters = new Dictionary<string, int> { { "key1", 1 }, { "key2", 2 } };
channel.CreateMetaCounters(metaCounters, (inMetaCounters, inError) =>
{
  if (inError != null)
    return; 
    // Handle error.
});

Update a metacounter

Copy link

The procedure of updating a channel metacounter is the same as creating a channel metacounter. Through the UpdateMetaCounters() method, values of existing keys are updated and values of new keys are added.

Dictionary<string, int> metaCounters = new Dictionary<string, int> { { "key1", 3 }, { "key2", 4 } };
channel.UpdateMetaCounters(metaCounters, (inMetaCounters, inError) =>
{
  if (inError != null)
    return; 
    // Handle error.
});

Increase values in a metacounter

Copy link

You can increase values in a channel metacounter by passing a Dictionary<string, int> of keys to increase as an argument to a parameter in the IncreaseMetaCounters() method. The values of corresponding keys in the metacounter are increased by the specified number.

Dictionary<string, int> metaCounters = new Dictionary<string, int> { { "key1", 5 }, { "key2", 2 } };
channel.IncreaseMetaCounters(metaCounters, (inMetaCounters, inError) =>
  {
  if (inError != null)
    return; 
    // Handle error.
   // If the value of key1 was 3, it's 8 now.
  });

Decrease values in a metacounter

Copy link

You can decrease values in a channel metacounter by passing a Dictionary<string, int> 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.

Dictionary<string, int> metaCounters = new Dictionary<string, int> 
{ 
    { "key1", 5 }, 
    { "key2", 2 } 
};

channel.DecreaseMetaCounters(metaCounters, (inMetaCounters, inError) =>
{
    if (inError != null)
        return; //Handle error.

    // if the value of key1 was 8, it's 3 now.
});

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 Dictionary<string, int> collection is returned with the corresponding key-value items.

channel.GetMetaCounters(new List<string> { "key1", "key2" }, (inMetaCounters, inError) =>
{
    if (inError != null)
        return; //Handle error.
});

Delete a metacounter

Copy link

You can delete a channel metacounter as shown below.

channel.DeleteMetaCounters("key1", (inError) =>
{
    if (inError != null)
        return; //Handle error.
});