简体   繁体   English

在Table View中对核心数据进行汇总操作,并按日期进行计数和分组

[英]Aggregate Operation on Core Data in Table View with count and grouping by date

I have a Core Data database with an entity "Event" and an attribute "eventDate" with "Date" type. 我有一个核心数据数据库,其中包含一个实体“ Event”和一个属性“ eventDate”,其类型为“ Date”。 The task is showing events grouping by date and showing the number (@"count") of events date by date. 任务是按日期显示事件分组,并按日期显示事件的编号(@“计数”)。 For example eventDate (mm-dd-yyyy) data: 例如eventDate(mm-dd-yyyy)数据:

events     eventDate
event 1    01-01-2013
event 2    01-01-2013
event 3    01-02-2013
event 4    01-03-2013
...

Result in table view: 表格视图中的结果:

date         count
01-01-2013     2
01-02-2013     1
01-03-2013     1
...

Don't like to fetch all database as it could be big so I use aggregate operation not to fetch every object. 不喜欢获取所有数据库,因为它可能很大,所以我使用聚合操作而不是获取每个对象。 The code is working without error but the resulting array is empty. 该代码正常工作,但结果数组为空。 My guess the problem is the grouping with date type attribute and the NSAttribute/attributeType but couldn't make it work. 我猜问题是带日期类型属性和NSAttribute / attributeType的分组,但是无法正常工作。 The code in the Event's category file is: 事件类别文件中的代码是:

+ (NSArray *)aggregateOperationOnEvent:(NSString *)attributeName withFunction:(NSString *)function withPredicate:(NSPredicate *)predicate inManagedObjectContext: (NSManagedObjectContext*)context {

NSEntityDescription* entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];

NSAttributeDescription* attributeDesc = [entity.attributesByName objectForKey:attributeName];


NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: attributeName];
NSExpression *functionExpression = [NSExpression expressionForFunction: [NSString stringWithFormat:@"%@:", function] arguments: [NSArray arrayWithObject:keyPathExpression]];

NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: function];
[expressionDescription setExpression: functionExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];


NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Event"];

[request setPropertiesToFetch:[NSArray arrayWithObjects:attributeDesc, expressionDescription, nil]];
[request setPropertiesToGroupBy:[NSArray arrayWithObject:attributeDesc]];
[request setResultType:NSDictionaryResultType];

if (predicate != nil)
    [request setPredicate:predicate];

NSError* error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];

return results;

} }

Aggregating on a NSDate is not a good idea, since it's backed by a floating point type. NSDate上聚合不是一个好主意,因为它有浮点类型的支持。 Things might be easier if you store something like days since reference date as an int32_t . 如果将自引用日期以来的天数存储为int32_t事情可能会更容易。 You need to use NSDateComponents : 您需要使用NSDateComponents

@implementation NSDate (Days)

-  (NSInteger)daysSinceReferenceDate;
{
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comp = [cal components:NSDayCalendarUnit fromDate:[NSDate dateWithTimeIntervalSinceReferenceDate:0] toDate:self options:0];
    return [comp day];
}

@end

The only problem was when I transferred the "results" array to another array the latter's allocation code accidentally commented out, this is why I got empty array in the NSLog. 唯一的问题是,当我将“结果”数组转移到另一个数组时,后者的分配代码被意外注释掉了,这就是为什么我在NSLog中得到空数组。 So the GOOD NEWS it's possible to use aggregate operation on DATE attribute and the solution is above. 因此,GOOD NEWS可以对DATE属性使用聚合操作,并且解决方案如上。 The "results" array's printout: “结果”数组的打印输出:

 2013-02-13 19:19:48.063 YourAppsName[35147:c07] -------- results = (
    {
    count = 2;
    eventDate = "2013-01-01 00:00:00 +0000";
},
    {
    count = 1;
    eventDate = "2013-01-02 00:00:00 +0000";
},
    {
    count = 1;
    eventDate = "2013-01-03 00:00:00 +0000";
}

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM