如何处理MUC聊天消息 - 消息重复 [英] How to handle MUC chat messages- messages duplicating
问题描述
我使用XMPP框架实现了一对一聊天它在一对一聊天中得到了广泛的支持。消息归档和提取非常简单。但我发现,处理群聊聊天消息很难保存和显示。排序和谓词都失败了。显示重复的消息。
I have implemented one to one chat using XMPP framework It has extensive support in one to one chat. Message archiving and fetching is way simple. But i see, very difficulty in handling a group chat message saving and displaying. The sorting and predicates are failing. duplicate messages are shown.
这是我在获取已保存的邮件之前加入房间的方式。)
This is how i join a room before i fetch my already saved message)
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次。我可能做错了什么。有些人帮忙!这是我在房间中记录消息的代码
I see, there are several redundant saving of messages. A single message is saved 3-4 times. What might i be doing wrong. Some body pls help! This is the code i do for fecthing messages in a room
- (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的子元素。收到消息后,我检查子元素。如果devce id匹配,它是我之前发送的相同消息,它已经在核心数据中,所以丢弃该消息。
I think i found out an answer for the message duplication issue. The main issue was the message i am sending in the room was repeated on each room joining. What i did was when ever i sent a message i kept a deviceID as child element for the xmppmessage. On recieving on the message, i check the child element. If the devce id matches, its the same message i send earlier and its already in the core Data, So discard the message.
- (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中,有一个名为
Then on message recieving in xmppstream. handle it In XMPPRoomCoreDataStorage, there is a method called
- (void)handleIncomingMessage:(XMPPMessage *)message room:(XMPPRoom *)room
关于这个做消息排序逻辑。不粘贴整个代码:
on this do the message sorting logic. Not pasting the entire code:
- (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
}
这篇关于如何处理MUC聊天消息 - 消息重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!