功能描述
IM SDK 提供获取会话的接口,可以获取指定的单个、多个会话的
V2TIMConversation
对象信息。获取指定的单个会话
示例代码如下:
String conversationID = "conversationID";V2TIMManager.getConversationManager().getConversation(conversationID, new V2TIMValueCallback<V2TIMConversation>() {@Overridepublic void onSuccess(V2TIMConversation v2TIMConversation) {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
[V2TIMManager.sharedInstance getConversation:@"conversationID" // 要查询的会话 IDsucc:^(V2TIMConversation *conv) {// 获取成功,返回 V2TIMConversation 对象} 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_;};V2TIMString conversationID = u8"conversationID";auto callback = new ValueCallback<V2TIMConversation>{};callback->SetCallback([=](const V2TIMConversation& conversation) {// 获取单个会话成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取单个会话失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->GetConversation(conversationID, callback);
获取指定的多个会话
示例代码如下:
List<String> conversationIDList = new ArrayList<>();conversationIDList.add("conversationID1");conversationIDList.add("conversationID1");V2TIMManager.getConversationManager().getConversationList(conversationIDList, new V2TIMValueCallback<List<V2TIMConversation>>() {@Overridepublic void onSuccess(List<V2TIMConversation> conversationList) {Log.i("imsdk", "success");}@Overridepublic void onError(int code, String desc) {Log.i("imsdk", "failure, code:" + code + ", desc:" + desc);}});
[V2TIMManager.sharedInstance getConversationList:@[@"convID1", @"convID2"] // 要查询的会话 ID 列表succ:^(NSArray<V2TIMConversation *> *list) {// 获取成功,list 中包含 V2TIMConversation 对象} 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"conversationID1");conversationIDList.PushBack(u8"conversationID2");auto callback = new ValueCallback<V2TIMConversationVector>{};callback->SetCallback([=](const V2TIMConversationVector& conversationList) {// 获取多个会话成功delete callback;},[=](int error_code, const V2TIMString& error_message) {// 获取多个会话失败delete callback;});V2TIMManager::GetInstance()->GetConversationManager()->GetConversationList(conversationIDList, callback);