【问题标题】:Fetch request to order conversations by date of most recent message获取按最新消息日期排序对话的请求
【发布时间】:2015-11-06 11:13:35
【问题描述】:

我有一个核心数据模型,它有一个对话实体和一个消息实体。会话将与 Message 对象具有一对多的关系,如下所示:

Conversation:
    messages: many Message

Message:
    sentDate: NSDate
    conversation: one Conversation

希望这是有道理的。

我要做的是列出所有对话,按每个对话中最近发送日期的消息排序。

是否有一个我可以执行的提取请求可以做到这一点?

【问题讨论】:

  • 如何获取?您可以使用NSSortDescriptor 添加到您的提取中。

标签: ios objective-c swift core-data nspredicate


【解决方案1】:

您可以使用关系和日期的 MAX 聚合来完成此操作。

NSFetchRequest *fetchRequest = [NSFetchRequest
    fetchRequestWithEntityName:@"Conversation"];
fetchRequest.sortDescriptors = @[[NSSortDescriptor
    sortDescriptorWithKey:@"messages.@max.sentDate"
                ascending:NO]];

【讨论】:

  • 这会产生错误Keypath containing KVC aggregate where there shouldn't be one; failed to handle messages.@max.sentDate
猜你喜欢
  • 2017-05-13
  • 1970-01-01
  • 2019-02-21
  • 2020-03-31
  • 2019-05-10
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多