ios - CoreData most recent entries in group -


i'm wondering if there's (simple) way achieve following using nsfetchedresultscontroller:

say have visitor log table this:

visitor_name (string) visit_time (date) visited_building_id (string) 

example data:

john, 2015-09-25 10:00:00, abc jane, 2015-09-25 10:20:00, abc mark, 2015-09-25 10:40:00, abc jane, 2015-09-25 10:10:00, def 

and want show table view shows visited each building, data above should show 2 rows:

[abc, mark, 9/25 10:40] [def, jane, 9/25 10:10] 

one solution to:

  1. sort entries visited_building_id (ascending or descending prefer) , visit_time (descending).
  2. specify visited_building_id sectionnamekeypath fetched results controller. frc therefore have section each building, , first object in each section recent visit. want table view rows display first item in each section of frc. so...
  3. amend tablewview datasource methods remap frc sections tableview's rows. example numberofrowsinsection return frc.sections.count. cellforrowatindexpath construct new nsindexpath using

    nsindexpath *newindexpath = [nsindexpath indexpathforrow:0 insection:(indexpath.row)]

    or

    let newindexpath = nsindexpath(forrow:0, insection:indexpath.row)

    and uses newindexpath obtain object display in cell using frc's objectatindexpath method.

note remapping must repeated/reversed in other datasource methods. , means tableview can have 1 section.


Comments