核心数据实体,上面有两个属性:startDay
和startDateTime
。这两个属性都是NSDate对象。 startDay
将其时间组件设置为午夜,startDateTime
指定带有时间的日期。 startDay
和startDateTime
中的“日历日”都相同。
以下是一些对象
(
{
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";
}
)
需要按startDay
分组对象。需要知道“早晨”或“下午”是否有活动。
以下是按startDay
分组的结果,并报告了最早和最新的每日活动。
- (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);
}
该代码正在返回
(
{
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";
}
)
如果它看起来像这样会更酷......
(
{
eveningEvent = YES;
morningEvent = YES;
startDay = "2014-05-27 00:00:00 +0000";
},
{
eveningEvent = NO;
morningEvent = YES;
startDay = "2014-05-28 00:00:00 +0000";
}
)
如何修改我的获取请求以将日期比较转换为NSExpression
(或NSExpressionDescription
?),以便核心数据检查min/maxEventStartTime
是否在中午之前/之后,以及返回BOOL而不是实际的NSDate
对象。