简体   繁体   English

iOS核心数据:NSPredicate筛选具有属性最大值的对象

[英]iOS Core Data: NSPredicate to filter objects with max value of a property

I have Members table, for example with the following entries: 我有Members表,例如,带有以下条目:

Name | Group | JoinDate
Member1 | Group1 | 2011-03-12
Member2 | Group2 | 2011-03-10
Member3 | Group1 | 2011-03-13
Member4 | Group3 | 2011-03-9
Member5 | Group2 | 2011-03-23

I want to fetch the most recent member from each group using NSFetchedResultController. 我想使用NSFetchedResultController从每个组中获取最新成员。 In the above case, the result will be: 在上述情况下,结果将是:

Name | Group | JoinDate
Member3 | Group1 | 2011-03-13
Member5 | Group2 | 2011-03-23
Member4 | Group3 | 2011-03-09

I was thinking maybe it would involve some complex NSPredicate. 我当时在想可能会涉及到一些复杂的NSPredicate。 Any idea how to do this? 任何想法如何做到这一点? Thanks! 谢谢!

Finding the most recent in each group all in one operation seems difficult, so break it down. 在一个操作中找到每个组中的最新操作似乎很困难,因此请将其分解。 It's easy to find the most recent member in a single group, so iterate over the set of groups and pick the most recent member out of each group. 在单个组中找到最新成员很容易,因此可以遍历一组组并从每个组中选择最新成员。

If you have a Group entity that's related to Member, so that you can fetch the sets of members in each group, then you could use KVC's '@max' collection operator to easily select the most recent join date from each of those sets. 如果您有一个与Member相关的Group实体,则可以获取每个组中的成员集,则可以使用KVC的'@max'集合运算符轻松地从每个集合中选择最近的加入日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM