Objective c 将核心数据计数到多个关系

Objective c 将核心数据计数到多个关系,objective-c,core-data,Objective C,Core Data,我试图在核心数据中通过聚合函数进行排序。我有一个模型,它有实体食谱和食物,它们之间的关系是多对多的。我从一个查询开始,该查询列出了包含我列出的食物的食谱。到目前为止,我得到了: NSExpression* key_path_exp = [NSExpression expressionForKeyPath:@"recipeName"]; NSExpression *countExpression = [NSExpression expressionForFunction: @"count:"

我试图在核心数据中通过聚合函数进行排序。我有一个模型,它有实体食谱和食物,它们之间的关系是多对多的。我从一个查询开始,该查询列出了包含我列出的食物的食谱。到目前为止,我得到了:

NSExpression* key_path_exp = [NSExpression expressionForKeyPath:@"recipeName"];
NSExpression *countExpression =  [NSExpression expressionForFunction: @"count:"
                                                           arguments: [NSArray arrayWithObject:key_path_exp] ];

// Count the number of specified foods that this recipe contains, by counting the grouped by recipeName
NSExpressionDescription *count_description = [[NSExpressionDescription alloc] init];
[count_description setName: @"count"];
[count_description setExpression: countExpression];
[count_description setExpressionResultType: NSInteger32AttributeType];

// Which of the columns I'm interested in
NSAttributeDescription* recipe_name_column = [_fetchRequest.entity.attributesByName objectForKey:@"recipeName"];
[_fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:recipe_name_column, count_description,  nil]];
// Group by the recipe name
[_fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:recipe_name_column]];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K IN %@",@"foods",foods];
[_fetchRequest predicate];
// Required for group by
[_fetchRequest setResultType:NSDictionaryResultType];

NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"healthFactor" ascending:NO];
[_fetchRequest setSortDescriptors:@[sortDescriptor]];

NSError *error = nil;
if (![_fetchedResultsController performFetch:&error])
{
    // Update to handle the error appropriately.
    NSLog(@"Unresolved error %@, %@", error.localizedDescription, [error userInfo]);
    exit(-1);  // Fail
}
这将生成SQL:

SELECT t0.ZRECIPENAME, COUNT( t0.ZRECIPENAME) FROM ZRECIPE t0 GROUP BY  t0.ZRECIPENAME ORDER BY t0.ZHEALTHFACTOR DESC
以及一个输出示例:

Old-school venison pie with juniper, rosemary & bay|5
Oriental pork with noodles|7
Pad Thai|3
Pint of prawns|10
Potato Gnocchi|7
Pumpkin Flan Recipe|12
当直接连接到sqlite数据库时,我可以编辑查询,使
orderby
变成:

ORDER BY count(t0.ZRECIPENAME) ASC
这正是我想要的结果。但是,如何使用
NSSortDescriptor
在核心数据中实现这一点呢


在创建模型之前,有人能提出一种根据结果集进行排序的方法吗
nssortdescriptio
只允许我按实体的任何一个键进行排序。或者,除了根据
NSDictionary
结果进行排序之外,还有更有效的排序解决方案。结果集可以是1000行。谢谢

尝试使用方便的方法
或使用比较器

    [<<insert Array or Dictionary>> sortUsingComparator:^NSComparisonResult(ObjectA *objectA, ObjectB *objectB) {
        NSString *titleA = nil;
        NSString *titleB = nil;

        titleA = [objectA valueForKey:@"titleForObjectA"];
        titleB = [objectB valueForKey:@"titleForObjectB"];

        return [titleA localizedStandardCompare:titleB];
    }];
[sortUsingComparator:^n比较结果(ObjectA*ObjectA,ObjectB*ObjectB){
NSString*titleA=nil;
NSString*titleB=nil;
titleA=[objectA valueForKey:@“titleForObjectA”];
titleB=[objectB valueForKey:@“titleForObjectB”];
返回[标题本地化标准比较:标题B];
}];
更新:抱歉这是
NSMutableArray
的一种方便方法

虽然对于
NSDictionary
,也有类似的方法

    [<<insert Array or Dictionary>> sortUsingComparator:^NSComparisonResult(ObjectA *objectA, ObjectB *objectB) {
        NSString *titleA = nil;
        NSString *titleB = nil;

        titleA = [objectA valueForKey:@"titleForObjectA"];
        titleB = [objectB valueForKey:@"titleForObjectB"];

        return [titleA localizedStandardCompare:titleB];
    }];

keysSortedByValueUsingComparator
文档,因此您可以在这个方便的方法中添加一个类似于上述的comparator块?

这是我使用的方法,它的缺点是只对内存中的内容进行排序,尽管分批排序有助于提高性能。对sqlite查询本身进行排序会更好。