/ SDKs / iOS
SDKs
Chat SDKs iOS v4
Chat SDKs iOS
Chat SDKs
iOS
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 [String: Int] can be stored into an OpenChannel or a GroupChannel 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 a metacounter into a channel, create a dictionary of a [String: Int] object and add key-value items. Then pass the object as an argument to a parameter when calling the createMetaCounters(_:completionHandler:) method. You can put multiple key-value items in the dictionary.

let newMetaCounters = [
    "key1": 1,
    "key2": 2
]

channel.createMetaCounters(newMetaCounters) { metaCounters, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

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.

let metaCountersToUpdate = [
    "key1": 3,  // Update 1 to 3.
    "key2": 4,  // Update 2 to 4.
    "key3": 0   // Add a new key-value item.
]

channel.updateMetaCounters(metaCountersToUpdate) { metaCounters, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

Increase a metacounter

Copy link

You can increase values in a channel metacounter by passing a [String: Int] object of the items to increase as an argument to a parameter in the increaseMetaCounters(_:completionHandler:) method. The values of corresponding keys in the metacounter are increased by the specified number.

let deltaMetaCounters = [
    "key1": 2, // Increase by 2.
    "key2": 3  // Increase by 3.
]

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

Decrease a metacounter

Copy link

You can decrease values in a channel metacounter by passing a [String: Int] object of the items to decrease as an argument to a parameter in the decreaseMetaCounters(_:completionHandler:) method. The values of corresponding keys in the metacounter are decreased by the specified number.

let deltaMetaCounters = [
    "key1": 3, // Decrease by 3.
    "key2": 4  // Decrease by 4.
]

channel.decreaseMetaCounters(deltaMetaCounters) { metaCounters, error in
    guard error == nil else {
        // Handle error.
        return
    }
}

Retrieve a metacounter

Copy link

You can retrieve a channel metacounter by creating an 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 is returned through completionHandler with the corresponding key-value items.

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

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

Delete a metacounter

Copy link

You can delete a channel metacounter as shown below.

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