核心数据:按关系属性排序 [英] Core Data: Sort by Relationship's Attribute
问题描述
我正在构建一个 iPhone的本地消息应用程序的开源克隆,名为GitHub上的AcaniChat 。
我有一个会话
实体和一个 Message
实体, sentDate
属性。每个会话
可以有许多消息
。如何获取会话
,按其最旧的消息$ c排序,该
sentDate
$ c>?
I have a Conversation
entity and a Message
entity with a sentDate
attribute. Each Conversation
can have many Message
s. How do I fetch Conversation
s sorted by the sentDate
of it's oldest Message
?
推荐答案
我能想到的最好方法是在对话
实体,称为 lastMessageSentDate
,每次出现 Message
并将其添加到对话
,将对话的 lastMessageSentDate
设置为邮件的 sentDate
。我可能还想向 Conversation
实体添加一个名为 lastMessageText
的属性,因为我想显示在 ConversationsViewController
上进行对话的最后一条消息的文本(就像本机iPhone Messages应用程序所做的那样),这样做将使我不必再为消息
。
The best way I can think of doing this is by adding an attribute to the Conversation
entity called lastMessageSentDate
and, every time a Message
comes in and gets added to a Conversation
, setting that conversation's lastMessageSentDate
to that message's sentDate
. I'll also probably want to add an attribute called lastMessageText
to the Conversation
entity as well because I want to show the text of the last message for a conversation on the ConversationsViewController
(just like the native iPhone Messages app does), and doing so will save me from having to do another Core Data fetch for the Message
.
Actually, I just had an idea! Maybe I can sort Conversation
s by messages.@max.sortedDate
, according to Xcode Documentation: Key-Value Coding Programming Guide: Collection Operators.
这篇关于核心数据:按关系属性排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!