2013-01-03 29 views
9

Đây là phiên bản SQL của truy vấn Tôi muốn viết cho Core dữ liệu:Core Data tìm nạp Thuộc tính với Nhóm By Đếm

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

Cho đến nay những gì tôi có là:

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; 

này gần như mang lại cho tôi tất cả mọi thứ, tuy nhiên thay vì groupName là mối quan hệ nhóm tôi muốn chỉ định group.name - điều này có khả thi không?

Đánh dấu

+3

Bạn đã thử '[fetchGroupSummary setPropertiesToGroupBy: [NSArray arrayWithObject: @" group.name "]]'? –

+0

Martin, hoạt động hoàn hảo! Nếu bạn thêm nó như là một câu trả lời, tôi sẽ chấp nhận. – markpirvine

Trả lời

9

setPropertiesToGroupBy của NSFetchRequest chấp nhận một loạt các NSPropertyDescription hoặc NSExpressionDescription đối tượng, hoặc chuỗi keypath. Trong trường hợp của bạn, bạn có thể sử dụng chuỗi đường phím:

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

Tôi đang cố gắng nhóm một tập hợp kết quả bằng một thuộc tính nằm trong đó. Nhưng nó bị lỗi với lỗi ** Yêu cầu tìm nạp không hợp lệ: GROUP BY yêu cầu NSDictionaryResultType **. [This] (http://pastebin.com/JHv87nFd) là mã của tôi. Bạn có thể cho tôi biết tôi đang làm gì sai ở đây không? – Isuru

+1

@Isuru: 'fetchRequest.resultType = .DictionaryResultType'. Việc ánh xạ giữa các liệt kê C (Objective-) C và các enums Swift được mô tả ở đây: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html. –

+1

Tôi đã làm điều đó nhưng tôi gặp lỗi mới ngay bây giờ ** CHỌN mệnh đề trong truy vấn với thành phần GROUP BY chỉ có thể chứa các thuộc tính có tên trong hàm GROUP BY hoặc tổng hợp ** – Isuru

Các vấn đề liên quan