setGroupCounters
接口分别设置计数器 key1 和 key2 的值为 0。HashMap<String, Long> counters = new HashMap<>();counters.put("key1", 0);counters.put("key2", 0);V2TIMManager.getGroupManager().setGroupCounters("your group id", counters, new V2TIMValueCallback<Map<String, Long>>(){@Overridepublic void onError(int code, String desc) {Log.d(TAG, "set group counters fail");}@Overridepublic void onSuccess(Map<String, Long> stringLongMap) {Log.d(TAG, "set group counters succ");}});
NSDictionary *counters = @{@"key1": @(0),@"key2": @(0)};[V2TIMManager.sharedInstance setGroupCounters:@"your group id" counters:counters succ:^(NSDictionary<NSString *,NSNumber *> *groupCounters) {NSLog(@"set group counters succ");} fail:^(int code, NSString *desc) {NSLog(@"set group counters fail");}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};void setGroupCounters() {V2TIMString groupID = "your group id";V2TIMStringToInt64Map counters;counters.Insert("key1", 0);counters.Insert("key2", 0);auto callback = new ValueCallback<V2TIMStringToInt64Map>{};callback->SetCallback([=](const V2TIMStringToInt64Map &counters){// succ},[=](int error_code, const V2TIMString& error_message){// fail});V2TIMManager::GetInstance()->GetGroupManager()->SetGroupCounters(groupID, counters, callback);}
increaseGroupCounter
接口传入的递增变化量 value 为 2 后,最终 key1 的值变为 10。V2TIMManager.getGroupManager().increaseGroupCounter("your group id", "key1", 2, new V2TIMValueCallback<Map<String, Long>>(){@Overridepublic void onError(int code, String desc) {Log.d(TAG, "increase group counters fail");}@Overridepublic void onSuccess(Map<String, Long> stringLongMap) {Log.d(TAG, "increase group counters succ");}});
[V2TIMManager.sharedInstance increaseGroupCounter:@"your group id" key:@"key1" value:2 succ:^(NSDictionary<NSString *,NSNumber *> *groupCounters) {NSLog(@"increase group counters succ");} fail:^(int code, NSString *desc) {NSLog(@"increase group counters fail");}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};void increaseGroupCounters() {V2TIMString groupID = "your group id";V2TIMString key = "key1";int64_t value = 2;auto callback = new ValueCallback<V2TIMStringToInt64Map>{};callback->SetCallback([=](const V2TIMStringToInt64Map &counters){// succ},[=](int error_code, const V2TIMString& error_message){// fail});V2TIMManager::GetInstance()->GetGroupManager()->IncreaseGroupCounter(groupID, key, value, callback);}
decreaseGroupCounter
接口传入的递减变化量 value 为 2 后,最终 key1 的值变为 6。V2TIMManager.getGroupManager().decreaseGroupCounter("your group id", "key1", 2, new V2TIMValueCallback<Map<String, Long>>(){@Overridepublic void onError(int code, String desc) {Log.d(TAG, "decrease group counters fail");}@Overridepublic void onSuccess(Map<String, Long> stringLongMap) {Log.d(TAG, "decrease group counters succ");}});
[V2TIMManager.sharedInstance decreaseGroupCounter:@"your group id" key:@"key1" value:2 succ:^(NSDictionary<NSString *,NSNumber *> *groupCounters) {NSLog(@"decrease group counters succ");} fail:^(int code, NSString *desc) {NSLog(@"decrease group counters fail");}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};void decreaseGroupCounters() {V2TIMString groupID = "your group id";V2TIMString key = "key1";int64_t value = 2;auto callback = new ValueCallback<V2TIMStringToInt64Map>{};callback->SetCallback([=](const V2TIMStringToInt64Map &counters){// succ},[=](int error_code, const V2TIMString& error_message){// fail});V2TIMManager::GetInstance()->GetGroupManager()->decreaseGroupCounter(groupID, key, value, callback);}
getGroupCounters
分别获取计数器 key1 和 key2 的值。List<String> keyList = Arrays.asList("key1", "key2");V2TIMManager.getGroupManager().getGroupCounters("your group id", keyList, new V2TIMValueCallback<Map<String, Long>>() {@Overridepublic void onError(int code, String desc) {Log.d(TAG, "get group counters fail");}@Overridepublic void onSuccess(Map<String, Long> stringLongMap) {Log.d(TAG, "get group counters succ");}});
[V2TIMManager.sharedInstance getGroupCounters:@"your group id" keys:@[@"key1", @"key2"] succ:^(NSDictionary<NSString *,NSNumber *> *groupCounters) {NSLog(@"get group counters succ");} fail:^(int code, NSString *desc) {NSLog(@"get group counters fail");}];
template <class T>class ValueCallback final : public V2TIMValueCallback<T> {public:using SuccessCallback = std::function<void(const T&)>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;ValueCallback() = default;~ValueCallback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess(const T& value) override {if (success_callback_) {success_callback_(value);}}void OnError(int error_code, const V2TIMString& error_message) override {if (error_callback_) {error_callback_(error_code, error_message);}}private:SuccessCallback success_callback_;ErrorCallback error_callback_;};void getGroupCounters() {V2TIMString groupID = "your group id";V2TIMStringVector keys;keys.PushBack("key1");keys.PushBack("key2");auto callback = new ValueCallback<V2TIMStringToInt64Map>{};callback->SetCallback([=](const V2TIMStringToInt64Map &counters){// succ},[=](int error_code, const V2TIMString& error_message){// fail});V2TIMManager::GetInstance()->GetGroupManager()->GetGroupCounters(groupID, keys, callback);}
setGroupCounters
、increaseGroupCounter
、decreaseGroupCounter
接口修改群计数器时,会触发 onGroupCounterChanged
回调,并返回变化后的 value 值。private void initListener() {if (groupListener == null) {groupListener = new V2TIMGroupListener() {@Overridepublic void onGroupCounterChanged(String groupID, String key, long newValue) {StringBuilder stringBuilder = new StringBuilder();stringBuilder.append("onGroupCounterChanged groupID:").append(groupID).append("\\n");stringBuilder.append("key:").append(key).append(", newValue:").append(String.valueOf(newValue)).append("\\n");String result = "onGroupCounterChanged :" + stringBuilder.toString();Log.d(TAG, result);}};V2TIMManager.getInstance().addGroupListener(groupListener);}}
[V2TIMManager.sharedInstance addGroupListener:self];#pragma mark - V2TIMGroupListener- (void)onGroupCounterChanged:(NSString *)groupID key:(NSString *)key newValue:(NSInteger)newValue {NSLog(@"groupID:%@, changed:\\n%@:%zd\\n", groupID, key, newValue);}
class GroupListener final : public V2TIMGroupListener {public:GroupListener() = default;~GroupListener() override = default;void OnGroupCounterChanged(const V2TIMString &groupID, const V2TIMString &key, int64_t newValue) override {// changed}};GroupListener listener;V2TIMManager::GetInstance()->AddGroupListener(&listener);
setGroupCounters
、increaseGroupCounter
、decreaseGroupCounter
接口合并计算,SDK 限制为单个登录用户最多 5 秒调用 20 次,超过限制后接口回调 8516 错误码;getGroupCounters
接口单独计算,SDK 限制为单个登录用户最多 5 秒 20 次调用,超过限制后接口回调 8516 错误码。
本页内容是否解决了您的问题?