Is there a particular way to configure an NSPredicate to compare dates?
Essentially I have a Photo object that has an NSDate, lastViewed.
I’d like to configure an NSPredicate that will return all the Photo objects that have been viewed more recently than a specified time period – typically two days.
I’m obtaining the past date like so:
NSTimeInterval secondsPast = -172800;
NSDate * twoDaysPast = [NSDate dateWithTimeInterval:secondsPast sinceDate:[NSDate date]];
And configuring the NSPredicate thusly:
request.predicate = [NSPredicate predicateWithFormat:@"lastViewed > %@", twoDaysPast];
However I’m getting no results back and I’m not quite certain why.
I know that all my Photo objects have lastViewed set – it’s set to a default value of now whenever the Photo is added to Core Data, so by default I should be seeing every Photo created as lastViewed will be more recent than my twoDaysPast NSDate.
Can I directly compare two instances of NSDate in this manner?
That is the proper way to use NSDates in NSPredicates. If it’s not working, have you tried removing the predicate altogether and making sure the rest of the request works? You also might want to sanity check your predicate by NSLog’ing it before running the query.