一尘不染

相当于核心数据的GROUP BY

sql

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

SELECT a_value
FROM my_table
GROUP BY a_value;

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

SELECT *
FROM my_table
GROUP BY a_value;

阅读 191

收藏
2021-03-10

共1个答案

一尘不染

这是模拟的

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];

在这里找到

2021-03-10