in my CoreData model I have a n2n relation modeled using an intermediate object:
Person [1<--] Person2Appointment [-->1] Appointment
The object is Person2Appointmentas follows:
@interface Person2Appointment : NSManagedObject
@property (nonatomic, retain) NSNumber * participationState;
@property (nonatomic, retain) Person * person;
@property (nonatomic, retain) Appointment * appointment;
...
@end
(both relations are also modeled as inverse relations for objects Personand Appointment)
I would like to receive an account of all past appointments for all persons.
In SQL, it will look like this:
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;
I tried to use the count-function expression with my select query as follows:
NSFetchRequest* fetch = [Person2Appointment fetchRequest];
fetch.resultType = NSDictionaryResultType;
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 = ...;
After enabling SQL logging, it seems that core-data is executing the correct 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 number, but instead the target is:
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 mistake in the master data?
Am I doing something wrong here?
?