2012-04-16 58 views
1

這是一個常見問題,我在這裏發現了許多相關的問題,但沒有解決這個問題。消息應用程序 - 對話lastMessage

我有這2款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
有沒有更好的方法來做到這一點?
因爲現在,只有一個地方我插入/更新新的消息,但如果有類似的插入/更新觸發會更好的聲音..

我知道我可以獲取所有對話,然後對其進行排序使用[email protected]的分類描述符,但對於大量消息/對話池來說聽起來不太合適。

回答

0

我真的不認爲有更優雅的東西或任何一種更好的方式,然後設置newMessage.conversation.lastMessage = newMessage。 核心數據發佈通知,每次它添加或更改實體時,但我不認爲,它會更容易觀察它們,然後每次創建新消息時添加此代碼行。

+0

我想這是有道理的。如果代碼多一點,也許可能有一個singleton觀察所有這些通知並進行更改(類似_trigger_),但在這種情況下聽起來有些開銷! – 2012-04-16 17:06:11