2014-03-04 48 views
1

我正在製作聊天應用程序,我想保存到核心數據用戶配置文件(圖像,名稱,年齡,詳細信息),並與其他用戶進行交談。在需要保存所有新消息時,我需要我可以看到消息已被讀取,如果用戶希望查看他的歷史記錄,我將從網絡服務器(如滾動到VC頂部)加載它。保存並加載核心數據中的xmpp消息

我使用該代碼來保存和加載消息,但是如何將結果數組用於構建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

向我們展示一些代碼:)您嘗試過哪些示例? – Marcio

+0

@marcio,我更新我的帖子,我需要結果數組將與消息,日期,圖像,用戶名開始聊天的2個用戶,但它不是 – user3358463

+0

所以你已經創建了你的數據模型?新增實體?處理下載的數據並保存到數據存儲中?獲取請求返回結果?你有多遠,對什麼不起作用更具體。 – Wain

回答

0

如果有人仍然需要一個答案:

只是這個謂詞添加到您的請求。

​​