简体   繁体   中英

Filter dates saved as String in CoreData

For learning purpose I have created a CoreData project and as expected for someone just started learning swift, I have been stuck quite often.

So far I have managed to understand how to save data to CoreDate and to display it in a table. But now I want to go a step further.

I have an Entity from few String (startDate, endDate,...) What is the right approach to retrieve the number of days per year, set be the intervals from start and end dates? for example if:

dateInterval1 = from Dec 30, 2014 to Jan 01, 2015 
dateInterval2 = from May 01, 2015 to May 10, 2015 
dateInterval3 = from Dec 30, 2015 to Jan 01, 2016

I want to count how many days from the intervals above are in 2015 (1+10+2)

I'm sorry for the question without any line of code. I'm playing in the playGround with Array [NSDate] converted to string, which become a bit messy, so decide the will be confusing to posted hear. Any help for the right approach will be much appreciated.

I want to count how many days from the intervals above are in 2015 (1+10+2)

You can't do this directly with Core Data. It doesn't support date math. It especially does not support date math when the dates are stored as strings .

The best you could do with the Core Data entity you describe is:

  1. Fetch all instances where either the start date or the end date is a string ending with "2015".
  2. For each fetched object, convert the string to an NSDate (in code, not automatically by Core Data) and then do your date math.

This would work but would be extremely inefficient. Step 1 involves comparing two strings for every object in your persistent store. Step 2 involves parsing two date strings for every fetched object. It's a lousy approach but it's the only one your entity allows for.

One approach that would be a lot better is:

  1. Change your entity to store dates as "Date" data instead of as strings.
  2. Fetch objects with dates in 2015. For the start date you'd do something like:

     let calendar = NSCalendar.currentCalendar() let startOf2015 = calendar.dateWithEra(1, year: 2015, month: 1, day: 1, hour: 0, minute: 0, second: 0, nanosecond: 0) 

    (Note that you may need to change the timeZone value on the calendar).

    Then you would make your Core Data predicate include something like startDate > %@ , with startOf2015 as the argument matching that %@ .

  3. Calculate how many days from startOf2015 to the startDate on each object:

     let startDate = myObject.startDate let components = calendar.components(NSCalendarUnit.CalendarUnitDay, fromDate: startOf2015!, toDate: startDate, options: NSCalendarOptions.allZeros) 

    Now components.day is how many days from one date to the other.

The process for endDate is basically the same.

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