9

これは、CoreData用に記述したいクエリのSQLバージョンです。

SELECT Group.Name, COUNT(Item.Name)
FROM Item INNER JOIN Group ON Item.GroupID = Group.ID
GROUP BY Group.Name

これまでのところ、私が持っているのは:

NSFetchRequest* fetchGroupSummary = [NSFetchRequest fetchRequestWithEntityName:@"Item"];

NSEntityDescription* entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:[[CoreDataManager sharedInstance] managedObjectContext]];

NSAttributeDescription* groupName = [entity.relationshipsByName objectForKey:@"group"];
NSExpression *countExpression = [NSExpression expressionForFunction: @"count:" arguments: [NSArray arrayWithObject:[NSExpression expressionForKeyPath: @"name"]]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];

[expressionDescription setName: @"count"];
[expressionDescription setExpression: countExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"group.sort" ascending:YES];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"group.stage == %@", stage];

[fetchGroupSummary setEntity:entity];
[fetchGroupSummary setSortDescriptors:@[sortDescriptor]];
[fetchGroupSummary setPropertiesToFetch:[NSArray arrayWithObjects:groupName, expressionDescription, nil]];
[fetchGroupSummary setPropertiesToGroupBy:[NSArray arrayWithObject:groupName]];
[fetchGroupSummary setResultType:NSDictionaryResultType];
[fetchGroupSummary setPredicate:predicate];

NSError* error = nil;
groups = [[[CoreDataManager sharedInstance] managedObjectContext] executeFetchRequest:fetchGroupSummary error:&error];

expressionDescription = nil;

これでほとんどすべてが得られますが、groupNameをグループ関係にする代わりに、group.nameを指定します-これは可能ですか?

マーク

4

1 に答える 1

9

setPropertiesToGroupByofは、またはオブジェクトの配列、またはキーパス文字列NSFetchRequestを受け入れます。あなたの場合、キーパス文字列を使用できます。NSPropertyDescriptionNSExpressionDescription

[fetchGroupSummary setPropertiesToGroupBy:[NSArray arrayWithObject:@"group.name"]];
于 2013-01-04T08:36:06.510 に答える