删除会话
功能描述
删除好友或退出群组后,SDK 不会自动删除对应的单聊或群聊会话。客户可以调用下文的接口,主动删除会话。
说明
删除会话多端同步功能仅 SDK 5.1.1 及以上版本支持。
删除单个会话
注意:
示例代码如下:
String conversationID = "conversationID";V2TIMManager.getConversationManager().deleteConversation(conversationID, new V2TIMCallback() {@Overridepublic void onSuccess() {// 删除会话成功}@Overridepublic void onError(int code, String desc) {// 删除会话失败}});
NSString *conversationID = @"conversationID";[[V2TIMManager sharedInstance] deleteConversation:conversationIDsucc:^{// 删除会话成功}fail:^(int code, NSString *desc) {// 删除会话失败}];
class Callback final : public V2TIMCallback {public:using SuccessCallback = std::function<void()>;using ErrorCallback = std::function<void(int, const V2TIMString&)>;Callback() = default;~Callback() override = default;void SetCallback(SuccessCallback success_callback, ErrorCallback error_callback) {success_callback_ = std::move(success_callback);error_callback_ = std::move(error_callback);}void OnSuccess() override {if (success_callback_) {success_callback_();}}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_;};V2TIMString groupName = u8"conversationID";auto callback = new Callback;callback->SetCallback([=]() {// 删除会话成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 删除会话失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->DeleteConversationGroup(groupName, callback);
删除多个会话
您可以调用
deleteConversationList
(Android / iOS & Mac / Windows) 接口,删除指定的会话。clearMessage 设置为 false 时,会保留会话消息;设置为 true 时,本地和服务器的消息会一起删除,并且不可恢复。说明:
仅 SDK 7.1 及以上版本支持。
每次最多支持删除 100 个会话。
示例代码如下:
List<String> conversationIDList = new ArrayList<>();conversationIDList.add("c2c_userID");conversationIDList.add("group_groupID");V2TIMManager.getConversationManager().deleteConversationList(conversationIDList, true, new V2TIMValueCallback<List<V2TIMConversationOperationResult>>() {@Overridepublic void onSuccess(List<V2TIMConversationOperationResult> results) {// 删除会话成功for (V2TIMConversationOperationResult result : results) {int code = result.getResultCode();String info = result.getResultInfo();String conversationID = result.getConversationID();}}@Overridepublic void onError(int code, String desc) {// 删除会话失败}});
NSMutableArray *conversationIDList = [NSMutableArray array];[conversationIDList addObject:@"c2c_userID"];[conversationIDList addObject:@"group_groupID"];[[V2TIMManager sharedInstance] deleteConversationList:conversationIDListclearMessage:truesucc:^(NSArray<V2TIMConversationOperationResult *> *results) {// 删除会话成功for (V2TIMConversationOperationResult *result in results) {int code = result.resultCode;NSString *info = result.resultInfo;NSString *conversationID = result.conversationID;}}fail:^(int code, NSString *desc) {// 删除会话失败}];
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_;};V2TIMStringVector conversationIDList;conversationIDList.PushBack(u8"c2c_userID");conversationIDList.PushBack(u8"group_groupID");auto callback = new ValueCallback<V2TIMConversationOperationResultVector>{};callback->SetCallback([=](const V2TIMConversationOperationResultVector& results) {// 删除会话成功for (size_t i = 0; i < results.Size(); ++i) {const V2TIMConversationOperationResult& result = results[i];int code = result.resultCode;V2TIMString info = result.resultInfo;V2TIMString conversationID = result.conversationID;}delete callback;},[=](int error_code, const V2TIMString& error_message) {// 删除会话失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->DeleteConversationList(conversationIDList, true, callback);
会话被删除通知
如果您事先调用了
addConversationListener
(Android / iOS & Mac / Windows) 添加会话监听器,就可以在 onConversationDeleted
中获取会话被删除的通知。示例代码如下:
@Overridepublic void onConversationDeleted(List<String> conversationIDList) {Log.i("imsdk", "onConversationDeleted");}
- (void)onConversationDeleted:(NSArray<NSString *> *)conversationIDList {// 会话被删除}
void OnConversationDeleted(const V2TIMStringVector &conversationIDList) override {// 收到会话被删除通知}