ios - 在 XMPPFrameWork ios 中将 XMPPMessage 保存在 CoreData 中

标签 ios save message xmppframework

我正在开发一个聊天应用程序并使用 xmppframework 。现在需要将消息存储在持久存储中。谁能告诉我如何在 IOS 中从 CoreData 存储和检索消息。

最佳答案

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

//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()];

要检索已保存的消息,这是我的项目中的示例代码:

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"

关于ios - 在 XMPPFrameWork ios 中将 XMPPMessage 保存在 CoreData 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20609824/

相关文章:

ios - didRegisterForRemoteNotificationsWithDeviceToken 从未在特定设备上调用

java - 保存具有复杂关系的实体时出现 StaleStateException

redirect - Struts 2如何显示保存在拦截器中的消息,该消息将重定向到另一个操作?

iOS Storyboard 可本地化字符串不适用于 UILabel 子类

ios - 我们可以出售 ios Enterprise 应用程序吗

iOS - 如何使用来自 NIB 的特定框架初始化自定义 UIView

pdf - 如何在 PDF 表单上保存填写的字段?

swift - putData 方法未被执行

scala - 在 scala 中将值从一个参与者传递到另一个参与者

java - 即使消息正确,TestNG 测试也会失败并显示错误消息