1

具有两个属性的核心数据实体:startDaystartDateTime. 这两个属性都是 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对象。

4

0 回答 0