CoreData通过另一关系(m2n)获取关系和组计数的请求 [英] CoreData fetch request for count of relation and group by another relation (m2n)

查看:142
本文介绍了CoreData通过另一关系(m2n)获取关系和组计数的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CoreData模型中我有一个使用中介实体建模的n2n关系:

in my CoreData model I have an n2n relationship modeled using an intermediary entity:

Person [1<--] Person2Appointment [-->1] Appointment

Person2Appointment 实体如下所示:

@interface Person2Appointment : NSManagedObject

// attributes
@property (nonatomic, retain) NSNumber * participationState;

// relations
@property (nonatomic, retain) Person * person;
@property (nonatomic, retain) Appointment * appointment;
...
@end

关系 Person 预约实体)

在SQL中,它将如下所示:

In SQL it would look like:

select count(fk_appointment), fk_person 
  from person2appointment t0
  left join appointment t1 on t0.fk_appointment = t1.pk
where t1.date < ...
group by fk_person;

我尝试使用带有我的fetch-request的计数函数表达式,如下:

I tried using a count-function expression with my fetch-request the following way:

 // create a fetch request for the Person2Appointment entity (category method)
NSFetchRequest* fetch = [Person2Appointment fetchRequest];
fetch.resultType = NSDictionaryResultType;

// count appointments
NSExpression* countTarget = [NSExpression expressionForKeyPath: @"appointment"];
NSExpression* countExp = [NSExpression expressionForFunction:@"count:" arguments: @[countTarget]];
NSExpressionDescription* countDesc = [[NSExpressionDescription alloc]init];
countDesc.name=@"count";
countDesc.expression = countExp;
countDesc.expressionResultType =  NSDecimalAttributeType;

fetch.propertiesToFetch = @["person", countDesc];
fetch.propertiesToGroupBy = ["person"];

fetch.predicate = ...;

打开SQL日志后,核心数据似乎执行正确的语句:

After turning on SQL logging it seems that core-data executes the right statement:

SELECT t0.ZPERSON, COUNT( t0.ZAPPOINTMENT) 
  FROM ZPERSON2APPOINTMENT t0 
  JOIN ZAPPOINTMENT t1 ON t0.ZAPPOINTMENT = t1.Z_PK 
  WHERE  t1.ZSTARTDATE > ? GROUP BY  t0.ZPERSON 

但是结果数组中的字典不包含数字计数, ,约会实体:

But the dictionaries in the result array do not contain a numeric count, but instead, an appointment entity:

Printing description of d:
{
    count = "0xd0000000000c0018 <x-coredata://BABCBD2E-05AB-4AA5-AC2B-2777916E4EDF/Appointment/p3>";
    person = "0xd0000000000c0016 <x-coredata://BABCBD2E-05AB-4AA5-AC2B-2777916E4EDF/Person/p3>";
}

这是核心数据中的错误吗?

Is this a bug in core-data?

我在这里做错了吗?

还有另外一种方法来实现这个?

Or is there another way to achieve this?

推荐答案

这看起来像是CoreData中的一个错误,但经过一些实验,我想你可以通过修改count表达式来实现你想要的,计数 c>> c> cs>实体的属性,而不是计算 ,因为关系是一个,除非你有null值?):

That looks to me like a bug in CoreData, but after a bit of experimentation, I think you can achieve what you want by amending the count expression, to count an attribute of your Person2Appointment entity, rather than counting the Appointment relationship (the count should be the same, since the relationship is to-one, unless you have null values?):

NSExpression* countTarget = [NSExpression expressionForKeyPath: @"participationState"];
NSExpression* countExp = [NSExpression expressionForFunction:@"count:" arguments: @[countTarget]];

如果您确实有空值需要担心,可以使用:

If you do have null values to worry about, you can use:

NSExpression *countTarget = [NSExpression expressionForEvaluatedObject];

这篇关于CoreData通过另一关系(m2n)获取关系和组计数的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆