NSFetchRequest没有按正确顺序获取聊天消息

时间:2013-04-04 05:40:29

标签: iphone objective-c core-data

我正在使用iphone中的核心数据实现聊天数据库。我创建了两个实体chatuser和chatmessage。并且chatuser和chatmessgae实体之间存在一对多的关系。对于一个聊天用户,可以有许多聊天消息。我正在存储像

这样的数据
NSFetchRequest *request = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Chatuser"    
                                                  inManagedObjectContext:self.managedObjectContext];
        [request setEntity:entity];   

        NSPredicate *pred = [NSPredicate predicateWithFormat:@"chatuser == %@", chatuser];
        [request setPredicate:pred];

        NSArray *userArray=[self.managedObjectContext executeFetchRequest:request error:&error];


        if ([userArray count] > 0){
            Chatuser *user = [userArray objectAtIndex:0];

            Chatmessage *message = [NSEntityDescription insertNewObjectForEntityForName:@"Chatmessage" inManagedObjectContext:self.managedObjectContext];
            message.chatmessage = chatmessage;

            [user addAllchatmessagesObject:message];
            NSError *error;

            if(![self.managedObjectContext save:&error])
            {
                NSLog(@"whoops, couldn't save: %@", [error localizedDescription]);
            }           

        }
//    }
    else 
    {
        Chatuser *user = [NSEntityDescription insertNewObjectForEntityForName:@"Chatuser" inManagedObjectContext:self.managedObjectContext];
        user.chatuser = chatUser;

        Chatmessage *message = [NSEntityDescription insertNewObjectForEntityForName:@"Chatmessage" inManagedObjectContext:self.managedObjectContext];
        message.chatmessage = chatmessage;

        [user addAllchatmessagesObject:message];
        NSError *error;

        if(![self.managedObjectContext save:&error])
        {
            NSLog(@"whoops, couldn't save: %@", [error localizedDescription]);
        }
    }

以这种方式获取数据

managedObjectContext = appDelegate.managedObjectContext;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Chatuser" inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];
    NSError *error;

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"chatuser == %@", appDelegate.chatUser];
    [request setPredicate:pred];

    NSArray *userinfo = [self.managedObjectContext executeFetchRequest:request error:&error];

    if([userinfo count]>0)
    {

        Chatuser *user = [userinfo objectAtIndex:0];
        NSSet *messageset = user.allchatmessages;
        self.messageinfo = [messageset allObjects];
        int message_count=[self.messageinfo count];
        NSLog(@"Message count : %d", message_count);
        for(int i=(message_count-1);i>=0;i--)
        {
            Chatmessage *chatmsgnew = [self.messageinfo objectAtIndex:i];
            [tableArray addObject:chatmsgnew.chatmessage];
        }
    }

现在问题是聊天消息不是按照我存储它们的顺序出现的。就像我正在存储消息1,2,3,4。虽然像2,1,4,3 ...... 我没有遇到问题。任何人都可以提出任何建议。

1 个答案:

答案 0 :(得分:0)

获取请求不一定按照它们插入数据库的顺序返回对象。如果您需要特定订单,则必须向实体添加Integer属性(例如messageId),并向获取请求添加排序描述符:

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"messageId"
         ascending:YES];
[request setSortDescriptors:@[sort]];

已添加:如果您获取Chatmessage个对象,则上述方法有效。因此,您可以先为Chatuser实体创建一个获取请求,而不是首先获取Chatmessage,而是使用类似

的谓词
[NSPredicate predicateWithFormat:@"user.chatuser == %@", appDelegate.chatUser]

(假设“user”是从 Chatmessage Chatuser 的反向关系),以及上面的排序描述符。

或者,如果你有Chatuser个对象,那么你可以像这样排序user.allchatmessages

NSSet *messageset = user.allchatmessages;
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"messageId"
         ascending:YES];
self.messageinfo = [[messageset allObjects] sortedArrayUsingDescriptors:@[sort]];