我正在制作聊天应用,我想保存到核心数据用户个人资料(图片,姓名,年龄,详细信息),这与其他用户进行了对话。在需要保存所有新消息时,我需要我可以看到消息被读取,如果用户想要查看他的历史记录,我将从Web服务器加载它(如滚动到VC顶部)。
我使用该代码来保存和加载消息,但是如何将我的结果数组用于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()];
在谈话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"
答案 0 :(得分:0)
如果有人仍然需要答案:
只需将此谓词添加到您的请求中即可。
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"bareJidStr = %@", user.jid];