在XMPPFrameWork iOS中的CoreData中保存XMPPMessage [英] Saving XMPPMessage in CoreData in XMPPFrameWork ios

查看:102
本文介绍了在XMPPFrameWork iOS中的CoreData中保存XMPPMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用聊天应用程序并使用xmppframework.现在需要将消息存储在持久性存储中.谁能告诉我如何在IOS中存储和检索CoreData中的消息.

I am working on a chat application and using xmppframework .Now there is a need for storing message in a persistent storage. Can anyone tell me how can i store and retrieve message from CoreData in IOS.

推荐答案

由于您使用的是github/robbiehanson/XMPPFramework,因此在初始化期间很容易同时存储传入和传出消息:

Since you're using github/robbiehanson/XMPPFramework, it's easy to get both your incoming and outgoing message stored during initializing:

//this code init your 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, here's the sample code in my project:

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 you get the NSArray with element type of "XMPPMessageArchiving_Message_CoreDataObject"

这篇关于在XMPPFrameWork iOS中的CoreData中保存XMPPMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆