Query Messages
Overview
You can call the
findMessages
API to query local message details by messageID
, including messages with status V2TIM_MSG_STATUS_LOCAL_REVOKED
(revoked) and V2TIM_MSG_STATUS_HAS_DELETED
(deleted).Typically, if you want to display a message list, you can only store the messageID in memory. When you need to display the message content, or to display more information through a long press/right-click on a message, you can call the
findMessages
API to obtain the details of specific messages, instead of loading the message objects into memory from the start, which will save memory.Note:
1. Only local messages can be queried, for example, received messages or historical messages pulled by API.
2. Audio-video group (AVChatRoom) messages cannot be queried, as they are not saved locally.
3. You can distinguish the status of messages by the status of V2TIMMessage.
Querying a Local Message
Sample code:
V2TIMManager.getMessageManager().findMessages(messageIDList, new V2TIMValueCallback<List<V2TIMMessage>>() {@Overridepublic void onSuccess(List<V2TIMMessage> v2TIMMessages) {}@Overridepublic void onError(int code, String desc) {}});
[V2TIMManager.sharedInstance findMessages:messageIDListsucc:^(NSArray<V2TIMMessage *> *msgs) {// Messages queried successfully} fail:^(int code, NSString *desc) {// Failed to query the messages}];
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_;};auto callback = new ValueCallback<V2TIMMessageVector>{};callback->SetCallback([=](const V2TIMMessageVector& messageVector) {// Messages queried successfullydelete callback;},[=](int error_code, const V2TIMString& error_message) {// Failed to query the messagesdelete callback;});V2TIMManager::GetInstance()->GetMessageManager()->FindMessages(messageIDList, callback);