gpt4 book ai didi

ios - 如何处理MUC聊天消息-消息重复

转载 作者:行者123 更新时间:2023-11-29 10:36:29 24 4
gpt4 key购买 nike

我已经使用 XMPP 框架实现了一对一聊天它在一对一聊天中得到了广泛的支持。消息归档和获取非常简单。但是我看到,处理群聊消息的保存和显示非常困难。排序和谓词失败。显示重复消息。

这就是我在获取已保存的消息之前加入房间的方式)

XMPPRoomCoreDataStorage *coreDataRoomStorage=[self appDelegate].xmppRoomCoreDataStorage;
XMPPRoom *room=[[XMPPRoom alloc]initWithRoomStorage:coreDataRoomStorage jid:user.bareJid];
[room activate:[self appDelegate].xmppStream];
[room addDelegate:[self appDelegate] delegateQueue:dispatch_get_main_queue()];
[room joinRoomUsingNickname:user.user_name history:nil];

我看到了,有好几条消息的冗余保存。一条消息会保存 3-4 次。我可能做错了什么。一些 body 请帮助!这是我为在房间中发送消息所做的代码

- (NSFetchedResultsController *)fetchedResultsController{
if (fetchedResultsController == nil)
{
NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_message];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
inManagedObjectContext:moc];
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"bareJidStr=%@",_thisRoom.roomJID.bare];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setPredicate:predicate];
[fetchRequest setFetchBatchSize:20];
fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:moc
sectionNameKeyPath:nil
cacheName:nil];
[fetchedResultsController setDelegate:self];

NSError *error = nil;
if (![fetchedResultsController performFetch:&error])
{
DDLogError(@"Error performing fetch: %@", error);
}
}
return fetchedResultsController;}

最佳答案

我想我找到了消息重复问题的答案。主要问题是我在房间里发送的消息在每个房间加入时都重复了。我所做的是,每当我发送一条消息时,我都会将 deviceID 作为 xmppmessage 的子元素。收到消息后,我检查了子元素。如果设备 ID 匹配,则它是我之前发送的相同消息并且它已经在核心数据中,因此丢弃该消息。

- (void)sendMessageWithBody:(NSString *)messageBody
{
if ([messageBody length] == 0) return;

NSXMLElement *body = [NSXMLElement elementWithName:@"body" stringValue:messageBody];
XMPPMessage *message = [XMPPMessage message];
[message addChild:body];


//device id is used, so that the my message element will be unique
NSString *uuidString=[UIDevice currentDevice].identifierForVendor.UUIDString;
NSXMLElement *myMsgLogic=[NSXMLElement elementWithName:@"myMsgLogic" stringValue:uuidString];
[message addChild:myMsgLogic];

[self sendMessage:message];
}

然后在 xmppstream 中接收消息。处理它在XMPPRoomCoreDataStorage中,有一个方法叫做

 - (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room

在这上面做消息排序逻辑。不粘贴整个代码:

- (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
{
XMPPLogTrace();

XMPPJID *myRoomJID = room.myRoomJID;
XMPPJID *messageJID = [message from];


NSString *uuidString=[UIDevice currentDevice].identifierForVendor.UUIDString;

NSString *messageLogic= [[message elementsForName:@"myMsgLogic"].firstObject stringValue];

if ([uuidString isEqualToString:messageLogic]) {
return;
}

//rest code is already there in the method
}

关于ios - 如何处理MUC聊天消息-消息重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26681309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com