核心数据:按关系属性排序
我正在 GitHub 上构建一个 iPhone 原生消息应用程序的开源克隆,名为 AcaniChat。
我有一个 Conversation
实体和一个带有 sentDate
属性的 Message
实体。每个Conversation
可以有许多Message
。如何获取按最早的消息
的sentDate
排序的对话
?
I'm building an open-source clone of iPhone's native Messages app called AcaniChat on GitHub.
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的最好方法是向名为
lastMessageSentDate
的Conversation
实体添加一个属性,并且每次Message
传入时添加到对话
,将该对话的lastMessageSentDate
设置为该消息的sentDate
。我可能还想向Conversation
实体添加一个名为lastMessageText
的属性,因为我想在上显示对话的最后一条消息的文本>ConversationsViewController
(就像原生 iPhone 消息应用程序一样),这样做将使我不必为Message
执行另一次核心数据获取。其实我只是有一个想法!也许我可以按
[email protected] 对
,根据Xcode 文档:键值编码编程指南:集合运算符。对话
进行排序]The best way I can think of doing this is by adding an attribute to the
Conversation
entity calledlastMessageSentDate
and, every time aMessage
comes in and gets added to aConversation
, setting that conversation'slastMessageSentDate
to that message'ssentDate
. I'll also probably want to add an attribute calledlastMessageText
to theConversation
entity as well because I want to show the text of the last message for a conversation on theConversationsViewController
(just like the native iPhone Messages app does), and doing so will save me from having to do another Core Data fetch for theMessage
.Actually, I just had an idea! Maybe I can sort
Conversation
s by[email protected]
, according to Xcode Documentation: Key-Value Coding Programming Guide: Collection Operators.