2013-01-03 10 views
9

Questa è la versione di SQL della query vorrei scrivere per Core Data:Core Data Recupero Proprietà con gruppo dal conte

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

Finora quello che ho è:

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; 

Questo quasi mi dà tutto, tuttavia invece di groupName è la relazione di gruppo vorrei specificare group.name - è possibile?

Mark

+3

Hai provato '[fetchGroupSummary setPropertiesToGroupBy: [NSArray arrayWithObject: @ "group.name"]]'? –

+0

Martin, ha funzionato perfettamente! Se lo aggiungi come risposta, accetterò. – markpirvine

risposta

9

setPropertiesToGroupBy di NSFetchRequest accetta un array di NSPropertyDescription o NSExpressionDescription oggetti o stringhe KeyPath. Nel tuo caso, è possibile utilizzare una stringa di percorso chiave:

[fetchGroupSummary setPropertiesToGroupBy:[NSArray arrayWithObject:@"group.name"]]; 
+0

Sto provando a raggruppare un risultato impostato da una proprietà che è in esso. Ma si blocca con l'errore ** Richiesta di recupero non valida: GROUP BY richiede NSDictionaryResultType **. [Questo] (http://pastebin.com/JHv87nFd) è il mio codice. Per favore, dimmi cosa sto facendo male qui? – Isuru

+1

@Isuru: 'fetchRequest.resultType = .DictionaryResultType'. La mappatura tra enumerazioni (Objective-) C e enumerazioni Swift è descritta qui: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html. –

+1

L'ho appena fatto, ma ora ricevo un nuovo errore ** Le clausole SELECT nelle query con i componenti GROUP BY possono contenere solo proprietà denominate in GROUP BY o funzioni aggregate ** – Isuru