我必须计数和分组更多的3个属性。 我正在拉我的头发,而分组和计数字段与日期(yyyy / mm / dd HH:mm:ss)我想按年 - 月 - 日分组,忽略时间。但我仍然需要时间的细节视图。时区在这里起着很大的作用。所以我只是保存它的utc格式。
i have to count and group on more that 3 properties. i'm pulling my hair while grouping and counting on field with date (yyyy/mm/dd HH:mm:ss) i want to group by year - month - day and ignore the time. but i still need the time for detail view. time zone plays a big role here. so i just save it in utc format.
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?
how can i can group by date (yyyy/MM/dd)? and be ok for converting to UTC?
提前感谢
ps:工作和无法获取。
ps: group by on a transient property doesnt work and cant be fetched.
推荐答案我不得不转换日期的本地时区与fecth结果控制器如下
i had to convert date on the fly for local timezone with the fecth result controller as follow
[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"];然后手动对它们进行分组,并且表视图现在使用数组而不是fetch result controller。
then manually group them and table view now use an array instead of fetch result controller.
更多推荐
按日期(yyyy / MM / dd)与NSFetchRequest组合
发布评论