这必须是一个常见的问题,我在SO中找到了许多相关的问题,但没有一个是解决这个问题的方法。
我有两个coredata模型:
@class Message;
@interface Conversation : NSManagedObject
@property (nonatomic, retain) NSString * number;
@property (nonatomic, retain) NSSet *messages;
@property (nonatomic, retain) Message *lastMessage;
和
@class Conversation;
@interface Message : NSManagedObject
@property (nonatomic, retain) NSString * account;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSString * message;
@property (nonatomic, retain) NSNumber * sentFlag;
@property (nonatomic, retain) NSString * status;
@property (nonatomic, retain) Conversation *conversation;
在我的ConversationListViewController
中,我获取lastMessage.date
命令的所有会话。
然后,ConversationViewController
接收Conversation
并列出其messages
。
现在我正在手动处理lastMessage
,所以每次我在ConversationViewController中添加新消息时,我也会更新conversation.lastMessage
。
有没有更好的方法呢?
因为现在,我只在一个地方插入/更新新消息,但如果有像插入/更新触发器这样的东西,听起来会更好。
我知道我可以获取所有会话,然后使用messages.@max.date
使用排序描述符对它们进行排序,但对于大量消息/会话来说,这听起来不是一个好的解决方案。
发布于 2012-04-16 21:18:29
我真的不认为有什么比设置newMessage.conversation.lastMessage = newMessage
更优雅或更好的方法了。Core Data每次添加或更改实体时都会发布通知,但我不认为观察它们会更容易,然后在每次创建新消息时添加这行代码。
https://stackoverflow.com/questions/10173779
复制相似问题