简体   繁体   中英

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

I have Members table, for example with the following entries:

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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