Search code examples
core-datansmanagedobjectcontextnsexpression

NSExpression is always returning zero


I have an entity called Rounds that has basic data about golf rounds. I am trying to calculate the number of rounds as well as the average score. However, every time I try and calculate these values it returns 0 (zero). There are no errors, and no crashes.

I have the following function in Rounds.m:

+(NSNumber *)aggregateOperation:(NSString *)function onAttribute:(NSString *)attributeName withPredicate:(NSPredicate *)predicate inManagedObjectContext:(NSManagedObjectContext *)context
{
    NSExpression *ex = [NSExpression expressionForFunction:function
                                 arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:attributeName]]];

    NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
    [ed setName:@"result"];
    [ed setExpression:ex];
    [ed setExpressionResultType:NSInteger64AttributeType];

    NSArray *properties = [NSArray arrayWithObject:ed];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setPropertiesToFetch:properties];
    [request setResultType:NSDictionaryResultType];

    if (predicate != nil)
        [request setPredicate:predicate];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Rounds"
                              inManagedObjectContext:context];
    [request setEntity:entity];

    NSArray *results = [context executeFetchRequest:request error:nil];
    NSDictionary *resultsDictionary = [results objectAtIndex:0];
    NSNumber *resultValue = [resultsDictionary objectForKey:@"result"];
    return resultValue;
}

I then call this method from my view controller to set the values of the labels for number of rounds and scoring average:

-(NSNumber*) scoringAverageCalc
{
    NSNumber *scoreAverage = [Rounds aggregateOperation:@"average:" onAttribute:@"roundScore" withPredicate:nil inManagedObjectContext:_managedObjectContext];
    return scoreAverage;
}

-(NSNumber*)countOfRounds
{
    NSNumber *roundCount = [Rounds aggregateOperation:@"count:" onAttribute:@"roundDate" withPredicate:nil inManagedObjectContext:_managedObjectContext];
    return roundCount;
}

Can someone please tell me why I am not getting the correct value?


Solution

  • I think NSExpression is overkill for simple sums. I would do it like this: fetch your rounds as normal managed objects (NSManagedObjectResultType) and then use KVC which should have just the aggregators you need.

    NSNumber *sum = [rounds valueForKeyPath:@"@sum.score"];
    NSNumber *avg = [rounds valueForKeyPath:@"@avg.score"];
    

    Simple, isn't it? Check it out here.