Core Data entity that has two properties on it: startDay
and startDateTime
. Both properties are NSDate objects. startDay
has its time components set to midnight, and startDateTime
designates a date with a time. Both “calendar days” within startDay
and startDateTime
are the same.
Here are some objects
(
{
startDateTime = "2014-05-27 08:00:00 +0000";
startDay = "2014-05-27 00:00:00 +0000";
},
{
startDateTime = "2014-05-27 13:00:00 +0000";
startDay = "2014-05-28 00:00:00 +0000";
}
)
Need to get objects grouped by startDay
. Need to know if there is event in the "morning" or "afternoon".
Here's how to get results grouped by startDay
and have earliest and latest daily events reported.
- (void)fetchEventDays
{
NSExpression *startDateTimeExpression = [NSExpression expressionForKeyPath:@"startDateTime"];
NSExpression *minStartDateTime = [NSExpression expressionForFunction:@"min:" arguments:[NSArray arrayWithObject:startDateTimeExpression]];
NSExpression *maxStartDateTime = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:startDateTimeExpression]];
NSExpressionDescription *minStartDateTimeExpression = [[NSExpressionDescription alloc] init];
minStartDateTimeExpression.name = @"minEventStartTime";
minStartDateTimeExpression.expression = minStartDateTime;
minStartDateTimeExpression.expressionResultType = NSDateAttributeType;
NSExpressionDescription *maxStartDateTimeExpression = [[NSExpressionDescription alloc] init];
maxStartDateTimeExpression.name = @"maxEventStartTime";
maxStartDateTimeExpression.expression = maxStartDateTime;
maxStartDateTimeExpression.expressionResultType = NSDateAttributeType;
NSManagedObjectContext *context = [RKObjectManager sharedManager].managedObjectStore.mainQueueManagedObjectContext;
NSEntityDescription* entity = [NSEntityDescription entityForName:@"NIModelScheduleData" inManagedObjectContext:context];
NSAttributeDescription* startDayDesc = [entity.attributesByName objectForKey:@"startDay"];
NSFetchRequest* fetch = [[NSFetchRequest alloc] init];
fetch.entity = entity;
fetch.propertiesToFetch = [NSArray arrayWithObjects:startDayDesc, minStartDateTimeExpression, maxStartDateTimeExpression, nil];
fetch.propertiesToGroupBy = [NSArray arrayWithObject:startDayDesc];
fetch.resultType = NSDictionaryResultType;
NSError *error = nil;
NSArray *results = [context executeFetchRequest:fetch error:&error];
NSLog(@"%@", results);
}
That code is returning
(
{
maxEventStartTime = "2014-05-27 13:00:00 +0000";
minEventStartTime = "2014-05-27 08:00:00 +0000";
startDay = "2014-05-27 00:00:00 +0000";
},
{
maxEventStartTime = "2014-05-28 10:00:00 +0000";
minEventStartTime = "2014-05-28 09:00:00 +0000";
startDay = "2014-05-28 00:00:00 +0000";
}
)
It'd be way cooler if it looked like this
(
{
eveningEvent = YES;
morningEvent = YES;
startDay = "2014-05-27 00:00:00 +0000";
},
{
eveningEvent = NO;
morningEvent = YES;
startDay = "2014-05-28 00:00:00 +0000";
}
)
How can I modify my fetch request to get a date comparison into my NSExpression
(or NSExpressionDescription
?), so that Core Data checks if min/maxEventStartTime
are before/after noon, and returns a BOOL instead of the actual NSDate
object.