I store time periods in Core Data. Each time period has an DateTime attribute called EndDate. I am trying to get the maximum end date, which is before (<) the date specified.

This is how I have coded this using a subquery and ValueForKeyPath:
NSString *keyPath = [NSString stringWithFormat:@"SUBQUERY(SELF, $x, $x.EndDate < %@).@max.EndDate", date];
IBFinPeriod *periodBeforeCurrentDate = [self.finperiod valueForKeyPath:keyPath];
However, when running this code, I get the runtime error: the entity IBFinPeriod is not key value coding-compliant for the key "SUBQUERY(SELF, $x, $x".'
What is wrong with my code?
Do I need to specify the subquery differently?
Thank you for your help!!
You could use a fetch request with
fetchLimitset to 1 and a descending sort descriptor.If you insist on the
valueForKeyPath:I would first filter the results withfilteredArrayUsingPredicate:(with a straight forward predicate selecting the records with dates prior to yourdate) and then simply using@"@max.EndDate"as the key path.If you need the entire object rather than just the date, just sort your set:
In my opinion it would be easier to just fetch.