这是核心数据专家之一,我猜。也许这超出了它应该达到的范围......无论如何:核心数据谓词和表情疯狂
在iOS上,使用sqlite持久性存储。
我有实体像这样: 甲< - >> B < < - “ç
B具有属性“V”,这是一个浮子,和另一属性“d”,这是一个日期。 B与A有单一关系,'C'与C单一关系。'
我可以计算所有B.v的平均值,其中B.a == someA,代码如下。但是,我真正想要做的是计算所有B.v的平均值,其中B.a == someA,其中B是C的最后一个B,由B.d.排序。所以平均函数对于每个C(最高版本)只会“拾取”一个B.有任何想法吗?
// create the fetch request
NSFetchRequest * request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"B" inManagedObjectContext:managedObjectContext]];
// create the expression
NSExpression * keyPathExpression = [NSExpression expressionForKeyPath:@"v"];
NSString * aggregationFunction = @"average:";
NSExpression * aggregationExpression = [NSExpression expressionForFunction:aggregationFunction arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription * expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"aggregateValue"];
[expressionDescription setExpression:aggregationExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@"a == %@", someA];
[request setPredicate:predicate];
[request setResultType:NSDictionaryResultType];
// Execute the fetch.
NSError * error = nil;
NSArray * objects = [managedObjectContext executeFetchRequest:request error:&error];
在数据模型图中是否有拼写错误?它看起来应该是'A <-->> B <<--> C'。 – TechZen
是的,正确的,谢谢! – Colin