XMPP,CoreData - 如何仅加载自身和目标用户消息?

Posted

技术标签:

【中文标题】XMPP,CoreData - 如何仅加载自身和目标用户消息?【英文标题】:XMPP, CoreData - how to load only self and destination user messages? 【发布时间】:2014-03-07 14:47:18 【问题描述】:

我保存到核心数据并使用下面的代码加载所有消息,但是我怎样才能只加载 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"

【问题讨论】:

【参考方案1】:

假设您的消息对象与User 实体具有from 关系。 就像添加一样简单(在执行请求之前):

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"from = %@ OR from = %@",selfUser,otherUser];

其中selfUserotherUserUser 核心数据托管对象或用户的托管对象ID。

如果您使用字段标识符(强烈不推荐):

 fetchRequest.predicate = [NSPredicate predicateWithFormat:@"bareJID.bare = %@ OR bareJID.bare = %@",selfUser.jid,otherUser.jid];

您可能想阅读THIS

【讨论】:

我使用 fetchRequest.predicate = [NSPredicate predicateWithFormat:@"bareJidStr = %@ OR bareJidStr = %@", self.sourceUser.uJid, self.destinationUser.uJid];但我仍然收到来自所有用户的消息 在消息条目中我没有从那个用户 ID 得到它是 我在问题正文中添加输出 我不太确定出了什么问题,您的输出并没有说明太多,也没有使用谓词。您需要在执行请求之前设置谓词。 问题可能出在 from="168@kotikiwebcrew.com/ms-MacBook-Air" 和 [NSPredicate predicateWithFormat:@"from = %@ OR from = %@", ?是否可以在没有详细信息的情况下从现场获取?【参考方案2】:

我使用了以下方法,它工作正常:)

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

        
    ];

【讨论】:

【参考方案3】:

你可以这样使用

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

【讨论】:

以上是关于XMPP,CoreData - 如何仅加载自身和目标用户消息?的主要内容,如果未能解决你的问题,请参考以下文章

如何获得 XMPP 子组?

在 Swift 上使用 CoreData 使用 Fetch Request 进行查询

如何将大量数据加载到 CoreData

在核心数据中保存和加载 xmpp 消息

如何使用 XMPP 进行广播?

使用 RestKit 和 CoreData 同步修改对象的正确方法