0

I want to get back unique dates with only days/months/year from source as: "2017-02-25 22:53:43" "2017-02-25 23:01:42" "2017-04-26 17:08:26" "2017-04-26 17:15:41"

column name is "date", the result should be [2017-02-25, 2017-02-26] if based days; [2017-02, 2017-04] if based on month; and [2017] based on years. I think need to use NSFetchRequest.predicate, but not sure how to do that.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ning
  • 148
  • 1
  • 10
  • This link explains in objectiveC, https://stackoverflow.com/questions/23704906/how-to-count-unique-dates-of-coredata-objects – Ning Jan 03 '18 at 21:19

1 Answers1

0

I would recommend using a fetchedResultsController and sectionNameKeyPath

  1. Add read-only properties to the managedObject subclass for day_month_year, month_year and year.
  2. in your fetchRequest sort by date
  3. for sectionNameKeyPath use one of the properties you created.
  4. the information you want is in the fetchedResultsController property sections

The method that you created for sectionNameKeyPath is called for every object. So don't do something expensive like create a NSCalendar.

Jon Rose
  • 8,373
  • 1
  • 30
  • 36