エンティティ「Event」と「Date」タイプの属性「eventDate」を持つCoreDataデータベースがあります。タスクは、日付ごとにグループ化されたイベントを表示し、日付ごとのイベントの数(@ "count")を表示します。たとえば、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
...
テーブルビューの結果:
date count
01-01-2013 2
01-02-2013 1
01-03-2013 1
...
すべてのデータベースが大きくなる可能性があるため、すべてのデータベースをフェッチするのは好きではありません。そのため、すべてのオブジェクトをフェッチするのではなく、集約操作を使用します。コードはエラーなしで機能していますが、結果の配列は空です。私の推測では、問題は日付型属性とNSAttribute / attributeTypeを使用したグループ化ですが、機能させることができませんでした。イベントのカテゴリファイルのコードは次のとおりです。
+ (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;
}