我们正在使用XMPP framwork进行聊天应用程序。我们使用“XMPPMessageArchiving_Message_CoreDataObject”来显示和存储聊天数据。但是有时多次收到一条消息,这个重复的消息存储在“XMPPMessageArchiving_Message_CoreDataObject”中。所以在显示时显示一条消息多次。
设置流方法使用以下代码启用此模块: -
xmppMessageArchivingStorage =
XMPPMessageArchivingCoreDataStorage.sharedInstance()
xmppMessageArchivingModule =
XMPPMessageArchiving(messageArchivingStorage :
xmppMessageArchivingStorage!)
xmppMessageArchivingModule?.clientSideMessageArchivingOnly = true
xmppMessageArchivingModule?.activate(xmppStream!)
xmppMessageArchivingModule!.addDelegate(self, delegateQueue:
DispatchQueue.main)
从DB获取数据: -
let storage = XMPPMessageArchivingCoreDataStorage.sharedInstance()
let moc: NSManagedObjectContext? =
storage?.mainThreadManagedObjectContext let entityDescription =
NSEntityDescription.entity(forEntityName:
"XMPPMessageArchiving_Message_CoreDataObject", in: moc ??
NSManagedObjectContext())
let request = NSFetchRequest<NSFetchRequestResult>.init(entityName:
"XMPPMessageArchiving_Message_CoreDataObject")
request.predicate = NSPredicate(format: "bareJidStr = %@ AND
streamBareJidStr = %@",
senderJID!,Constants.appDelegate.jabberID!)
request.entity = entityDescription
let messages = try! moc?.fetch(request) as! [AnyHashable]
如果有人有解决方案删除重复的聊天消息,请帮助我。
谢谢提前,
答案 0 :(得分:0)
嘿有两个步骤可以解决这个问题,但在此之前理解为什么你会收到重复的消息非常重要。如果Xmpp发送重复消息然后。可能会有一些其他配置问题,无论如何我们来解决您当前的问题。 将第二行代码添加到您的提取请求中以检索消息
let request = NSFetchRequest<NSFetchRequestResult>.init(entityName:
"XMPPMessageArchiving_Message_CoreDataObject")
request.returnsDistinctResults = true //Add this line
在将消息插入数据库时,您需要确认该特定ID是否存在消息,要在XMPP中为消息添加消息,您需要在向服务器发送消息时添加一些代码:
NSString *messageID = [XMPPStream generateUUID];
[self addAttributeWithName:@"id" stringValue:messageID];