【问题标题】:group by date (yyyy/MM/dd) with NSFetchRequest使用 NSFetchRequest 按日期(yyyy/MM/dd)分组
【发布时间】:2013-06-01 07:58:09
【问题描述】:

我必须对超过 3 个属性进行计数和分组。 我在用日期分组和计数字段时拉头发(yyyy/mm/dd HH:mm:ss) 我想按年-月-日分组并忽略时间。但我仍然需要时间查看详细信息。时区在这里起着重要作用。所以我只是把它保存为 utc 格式。

NSFetchRequest *fetchRequest    = [[NSFetchRequest alloc] init];
NSEntityDescription *entity     = [NSEntityDescription entityForName:@"logs"
                                              inManagedObjectContext:context;
NSAttributeDescription* att3 = [entity.attributesByName objectForKey:@"savedDate"];
NSExpression *keyPathExpression3 = [NSExpression expressionForKeyPath: @"savedDate"];
NSExpression *countExpression3 = [NSExpression expressionForFunction: @"count:"
                                                           arguments: [NSArray arrayWithObject:keyPathExpression3]];
NSExpressionDescription *att3Description = [[NSExpressionDescription alloc] init];
[expressionDescription3 setName: @"countSavedDate"];
[expressionDescription3 setExpression: countExpression3];
[expressionDescription3 setExpressionResultType: NSInteger32AttributeType];

...

[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:att1, att1Description, att2, att2Description, att3, att3Description, att4Description, att4, nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:att1, att2, att2, att3, nil]];
[fetchRequest setResultType:NSDictionaryResultType];

如何按日期分组 (yyyy/MM/dd)?可以转换为 UTC 吗?

提前致谢

ps:对瞬态属性进行分组不起作用且无法获取。

【问题讨论】:

标签: ios core-data nsfetchrequest


【解决方案1】:

我必须使用 fecth 结果控制器即时转换本地时区的日期,如下所示

[self willAccessValueForKey:@"startDateTime"];

NSDate * date = [self valueForKey:@"startDateTime"];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:[NSCalendar currentCalendar]];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDoesRelativeDateFormatting:YES];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSString *tmpValue = [formatter stringFromDate:date];
[self didAccessValueForKey:@"callStartDateTime"];

然后手动将它们分组,表格视图现在使用数组而不是获取结果控制器。

【讨论】:

    猜你喜欢
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多