我有一个视图,我希望获得“百分比”属性的平均值,而无需将所有对象(及其他属性)从核心数据加载到内存中。我已经发现如何在苹果的文档中做到这一点,但问题是我想限制百分比被平均化的对象为具有另一个名为“numberAsked”的属性大于0的对象。我认为这是我可以做的事情与NSPredicate。在核心数据中使用NSPredicate过滤setPropertiesToFetch?
以下是我有:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Statistics" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"numberAsked > 0"];
[request setPredicate:searchPredicate];
[request setResultType:NSDictionaryResultType];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"percentCorrect"];
NSExpression *averagePercentExpression = [NSExpression expressionForFunction:@"average:"
arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"averagePercentageCorrect"];
[expressionDescription setExpression:averagePercentExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
NSError *error;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
什么情况是,它似乎获取的所有对象,而忽略了NSPredicate。我不知道是否改变请求的结果类型对此做了些什么,或者我需要用另一个NSExpression或者什么来过滤对象。
我的测试值是,现在所有的对象都有数字被设置为0的属性,所以理论上我的最终对象数组应该有0的计数,从那里我会指定百分比的“N/A”。但是这并没有发生。
我感谢任何帮助!
我不知道这是否是我想要的东西,或者如果我很清楚... 我的代码与下面的Apple代码类似。我将NSExpressionDescriptions(不是NSExpressions)的数组传递给setPropertiesToFetch :.该代码位于“获取特定值”下: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdFetching.html#//apple_ref/doc/uid/TP40002484- SW6 我得到的百分比属性的平均值就好了,只是我不一定要平均所有的百分比 - 只是该对象的另一个属性(numAsked)> 0的对象的百分比。 – Kevin 2010-08-27 20:12:55