核心数据的GROUP BY等价物

smt*_*ire 7 sql iphone core-data objective-c

我知道我可以使用@distinctUnionOfObjects在SQL中找到类似下面的内容:

SELECT a_value
FROM my_table
GROUP BY a_value;
Run Code Online (Sandbox Code Playgroud)

我正在寻找的是数组中返回的所有数据,而不仅仅是与表达式匹配的值数组.基本上,我正在寻找核心数据的以下SQL查询的等价物:

SELECT *
FROM my_table
GROUP BY a_value;
Run Code Online (Sandbox Code Playgroud)

Bim*_*awa 17

这是模拟的

SELECT 'Status', COUNT(*) FROM 'Records' GROUP BY 'Status':

NSFetchRequest* fetch = [NSFetchRequest fetchRequestWithEntityName:@"Record"];
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Record"
                                          inManagedObjectContext:myManagedObjectContext];
NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:@"status"];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"url"]; // Does not really matter
NSExpression *countExpression = [NSExpression expressionForFunction: @"count:"
                                                          arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: @"count"];
[expressionDescription setExpression: countExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, expressionDescription, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
[fetch setResultType:NSDictionaryResultType];
NSError* error = nil;
NSArray *results = [myManagedObjectContext executeFetchRequest:fetch
                                                         error:&error];
Run Code Online (Sandbox Code Playgroud)

在这里找到