It is not necessary nor feasible to store a query inside a Core Data entity which itself has to be queried and retrieved. It's like writing a telephone number for enquiring about an address on the door of this address.
The cleanest way to do this is to simply keep the date as an attribute and then construct simple predicates to retrieve your List
objects for this date. All you need for this the beginning and the end of a certain day.
[NSPredicate predicateWithFormat:"dueDate > %@ && dueDate < %@", dayStart, dayEnd]
That's it. Your requirement "without more date manipulations" is not realistic, because you will need to calculate the start and end of day dynamically. It is nonsense to store this in Core Data's persistent store.
So to get the start of day of any date (written as a category on NSDate
):
-(NSDate *)startOfDay {
NSDateComponents *components = [[NSCalendar currentCalendar] components:
NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay
fromDate: self];
components.hour = 0; components.minute = 0; components.second = 0;
return [[NSCalendar currentCalendar] dateFromComponents:components];
}
And the end of any day
-(NSDate *)endOfDay {
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
return [[NSCalendar currentCalendar] dateByAddingComponents:components
toDate:self.startOfDay options:0];
}
For the predicate above you just substitute dayStart
and dayEnd
with [[NSDate date] startOfDay]
and [[NSDate date] endOfDay]
respectively.