简体   繁体   English

在核心数据中保存和加载xmpp消息

[英]Save and load xmpp messages in core data

I am making chat app and I want to save to core data user profile(images, name, age, detail info) and this talks with other users. 我正在制作聊天应用程序,我想保存到核心数据用户个人资料(图像,姓名,年龄,详细信息),并与其他用户进行交谈。 In need save all new messages, I need that I can see that messages was read, also if user will want to see his history I will load it from web serves (like scrolling to top of VC). 需要保存所有新消息时,我需要看到消息已被阅读,如果用户想查看他的历史记录,也可以从网络服务中加载它(例如滚动到VC顶部)。

I use that code to save and load messages, but how can I use my results array for construction chat history for 2 exact users? 我使用该代码保存和加载消息,但是如何将结果数组用于2个确切用户的构造聊天记录?

//this code in XMPPStream
 xmppStream = [[XMPPStream alloc]init];
[xmppStream addDelegate:self delegateQueue:dispatch_get_main_queue()];
 xmppStream.autoStartTLS = YES;

 xmppReconnect = [[XMPPReconnect alloc]init];
[xmppReconnect activate:self.xmppStream];

 xmppMessageArchivingCoreDataStorage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
 xmppMessageArchivingModule = [[XMPPMessageArchiving alloc]initWithMessageArchivingStorage:xmppMessageArchivingCoreDataStorage];
[xmppMessageArchivingModule setClientSideMessageArchivingOnly:YES];
[xmppMessageArchivingModule activate:xmppStream];    //By this line all your messages are stored in CoreData
[xmppMessageArchivingModule addDelegate:self delegateQueue:dispatch_get_main_queue()];

To retrieve the saved message in talks VC 在Talks VC中检索保存的消息

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

 NSManagedObjectContext *context = [self.xmppMessageArchivingCoreDataStorage mainThreadManagedObjectContext];
 NSEntityDescription *messageEntity = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject" inManagedObjectContext:context];

 fetchRequest.entity = messageEntity;

 NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:NO];
 fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
 NSError *error = nil;
 NSArray *results = [context executeFetchRequest:fetchRequest error:&error];
//Now I get the NSArray with element type of "XMPPMessageArchiving_Message_CoreDataObject"

if someone still needs an answer: 如果仍然需要答案:

just add this predicate to your request. 只需将此谓词添加到您的请求中即可。

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"bareJidStr = %@", user.jid];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM