0

I'm currently trying to set up a fetch request to retrieve events from a certain date range (starting with comparing just one date). After trying to create a NSPredicate with different variations of <=, => ,BETWEEN etc. which all did not work I spotted a possibly strange behaviour: My current NSPredicate looks like this :

let pred = NSPredicate(format: "start>= %@",dayBegin);

where dayBegin is set to NSDate();

However when printing the predicate I get:

start >= CAST(475632000.000000, "NSDate")

Evaluating the date above with

let date = NSDate(timeIntervalSince1970: NSTimeInterval(ti));

converts to yesterday, 31 ago.

The result is as expected, all dates in my calendar are shown (since all are above that date).

Trying the solution proposed here I experience the following parsing error:

'Unable to parse the format string "start>= 2016-01-28 00:00:00 +0000"'

I have also tried comparing the date as a time interval using %d but it didn't work as well. Any ideas whats going wrong?

Solved:

I was using an Accessor to modify the dates like this:

var startDate: NSDate?{
    get {
        return NSDate(timeIntervalSince1970: start);
        }
    set(d){
        start = d!.timeIntervalSince1970;
    }
}

Hence I always stored the days with the wrong reference days and did not notice That it refers to 1st of January 2001 instead of 1970.
Now Im using this Accessor:

var startDate: NSDate?{
    get {
    return NSDate(timeIntervalSinceReferenceDate: start);
    }
    set(d){
        start = d!.timeIntervalSinceReferenceDate;
    }
}
Community
  • 1
  • 1
hakkurishian
  • 266
  • 1
  • 3
  • 13
  • what's the device date set to? how did you create the dates in the data store? – Wain Jan 28 '16 at 14:35
  • im testing in simulator whose date is set to today. But on the device I get the same results. I Populated the dates like this: start = date.timeIntervalSince1970; The Managed Properties are from Type NSTimeInterval and the dates can be retrieved correctly – hakkurishian Jan 28 '16 at 14:35
  • 1
    Note that Core Data stores the time interval since the *refence date* (Jan 1, 2001 GMT), and that is what the value 475632000.000000 shows, it corresponds to `2016-01-28 00:00:00 +0000`. – Martin R Jan 28 '16 at 14:37
  • Oh, so I assume Im storing and retrieving the dates the wrong way , i did not encounter that – hakkurishian Jan 28 '16 at 14:39
  • `start = date.timeIntervalSince1970` is wrong. As explained in the "duplicate", it should be `start = date.timeIntervalSinceReferenceDate`. – Martin R Jan 28 '16 at 14:43

0 Answers0