2013-01-03 11 views
9

To jest wersja SQL kwerendy Chciałbym napisać dla danych Core:Rdzeń danych Pobieranie Properties z grupą przez hrabiego

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

tej pory co mam to:

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; 

ten prawie daje mi wszystko, jednak zamiast groupName będącego relacją grupy chciałbym podać group.name - czy to możliwe?

Mark

+3

próbowałeś '[fetchGroupSummary setPropertiesToGroupBy: [NSArray arrayWithObject: @ "group.name"]]'? –

+0

Martin, który działał idealnie! Jeśli dodasz to jako odpowiedź, zaakceptuję. – markpirvine

Odpowiedz

9

setPropertiesToGroupBy z NSFetchRequest akceptuje tablicę NSPropertyDescription lub NSExpressionDescription obiektów lub keypath łańcuchów. W twoim przypadku można użyć ciągu keypath:

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

Próbuję zgrupować zestaw wyników według właściwości, która jest w nim. Ale zawiesza się z powodu błędu ** Nieprawidłowe żądanie pobierania: GROUP BY wymaga NSDictionaryResultType **. [This] (http://pastebin.com/JHv87nFd) to mój kod. Czy możesz mi powiedzieć, co tu robię źle? – Isuru

+1

@Isuru: 'fetchRequest.resultType = .DictionaryResultType'. Mapowanie pomiędzy wyliczeniami (Objective-) C a wyliczeniami Swift jest opisane tutaj: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html. –

+1

Po prostu to zrobiłem, ale teraz otrzymuję nowy błąd ** klauzule SELECT w zapytaniach z komponentami GROUP BY mogą zawierać tylko właściwości nazwane w GROUP BY lub funkcje agregujące ** – Isuru