我保存到核心数据并使用下面的代码加载所有消息,但是如何仅为2个用户加载消息,对于自己的用户和目标用户,我希望将其基于用户JID。
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *context = [[self appDelegate].xmppMessageArchivingCoreDataStorage mainThreadManagedObjectContext];
NSEntityDescription *messageEntity = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject" inManagedObjectContext:context];
fetchRequest.entity = messageEntity;
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES];
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"
答案 0 :(得分:1)
假设您的消息对象与from
实体之间存在User
关系
就像添加(在执行请求之前)一样简单:
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"from = %@ OR from = %@",selfUser,otherUser];
其中selfUser
和otherUser
是User
核心数据管理对象或用户的托管对象ID。
如果您使用字段标识符(高度不推荐):
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"bareJID.bare = %@ OR bareJID.bare = %@",selfUser.jid,otherUser.jid];
您可能需要阅读THIS
答案 1 :(得分:1)
我使用了以下方法并且工作正常:)
-(void)checkForPastMessageAndDisplayIt
{
NSString *strmyid = @"user1@domain";
NSString *strBuddy = @"user2@domain";
NSManagedObjectContext *moc = [[self appDelegate] managedObjectContext_message_achiving];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
inManagedObjectContext:moc];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"bareJidStr = %@ OR bareJidStr = %@", strmyid, strBuddy];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *messagesArray = [moc executeFetchRequest:fetchRequest error:&error];
[messagesArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id messageObject = [messagesArray objectAtIndex:idx];
if ([messageObject isKindOfClass:[XMPPMessageArchiving_Message_CoreDataObject class]]) {
XMPPMessageArchiving_Message_CoreDataObject *currentMessageCoreDataObject = (XMPPMessageArchiving_Message_CoreDataObject*)messageObject;
XMPPMessage *message = currentMessageCoreDataObject.message;
if (message!=nil) {
[messages addObject:message];
}
}
}];
}
答案 2 :(得分:0)
你可以这样使用
-(void)testMessageArchiving {
NSString *strmyid = @"myid";
NSString *strBuddy ="toid";
NSLog(@"my id and buddy is is %@ and %@",strmyid,strBuddy);
XMPPMessageArchivingCoreDataStorage *storage = [XMPPMessageArchivingCoreDataStorage sharedInstance];
NSManagedObjectContext *moc = [storage mainThreadManagedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"XMPPMessageArchiving_Message_CoreDataObject"
inManagedObjectContext:moc];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
request.predicate = [NSPredicate predicateWithFormat:@"bareJidStr = %@ OR bareJidStr = %@", strmyid, strBuddy];
NSError *error;
NSArray *messages = [moc executeFetchRequest:request error:&error];
// NSLog(@"the history of messages is %@",messages);
[self print:[[NSMutableArray alloc]initWithArray:messages]];