0
我有我的聊天应用程序中的下列对象(一个一对多的关系)NSPredicate在集合中最新的项目
@interface ChatEntry : NSManagedObject
@property (nonatomic, retain) NSString * text;
@property (nonatomic, retain) NSDate * timestamp;
@property (nonatomic, retain) ChatContext *context;
@end
@interface ChatContext : NSManagedObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * userId;
@property (nonatomic, retain) NSSet *entries;
@end
每个会话(ChatContext)组的所有消息由该用户发送(唯一标识由userId字段)。
我试图显示会话列表,显示每个会话的最后一条消息(类似于iMessage)。
我用下面的代码:
NSFetchRequest* request=[[NSFetchRequest alloc] initWithEntityName:@"ChatEntry"];
request.predicate=[NSPredicate predicateWithFormat:@"[email protected]"];
request.sortDescriptors=[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:NO]];
m_chat=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:m_context sectionNameKeyPath:nil cacheName:@"chat"];
m_chat.delegate=self;
,直到我收到一个新的消息(与NSFetchedResultsChangeInsert)这工作得很好
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
...
}
在这一点上,我得到我的表中的另一个条目视图。 fetch控制器保留先前的条目(尽管它的时间戳不再是最新的条目)。我不知道如何解决这个问题,并迫使控制器总是评估谓词。
另外,我试图改变我的查询来获取ChatContext实例,但是我不确定如何实际读取该上下文中的最新条目。
你解决了吗? – 2013-07-31 22:59:21
查看我的答案 – 2013-08-02 01:44:47