Calculating the max value for a core data attribute - NSCFNumber error

前端 未结 3 1415
再見小時候
再見小時候 2020-12-16 15:23

I need to find out the maximum value of an attribute of a core data entity.

I\'m still firmly in the Cocoa learning curve, and this is a simple test app that I\'m us

相关标签:
3条回答
  • 2020-12-16 15:24

    This code from Core Data Programming Guide should work.

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];
    [request setEntity:entity];
    
    // Specify that the request should return dictionaries.
    [request setResultType:NSDictionaryResultType];
    
    // Create an expression for the key path.
    NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"creationDate"];
    
    // Create an expression to represent the maximum value at the key path 'creationDate'
    NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];
    
    // Create an expression description using the maxExpression and returning a date.
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    
    // The name is the key that will be used in the dictionary for the return value.
    [expressionDescription setName:@"maxDate"];
    [expressionDescription setExpression:maxExpression];
    [expressionDescription setExpressionResultType:NSDateAttributeType];
    
    // Set the request's properties to fetch just the property represented by the expressions.
    [request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
    
    // Execute the fetch.
    NSError *error = nil;
    NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
    if (objects == nil) {
        // Handle the error.
    }
    else {
        if ([objects count] > 0) {
            NSLog(@"Maximum date: %@", [[objects objectAtIndex:0] valueForKey:@"maxDate"]);
        }
    }
    
    0 讨论(0)
  • 2020-12-16 15:45

    I had to solve a similar problem and approached it slightly differently by:

    • querying the entity
    • ordering by the attribute you want
    • limit the fetch results to 1

    The code for this is below. I am querying an entity called 'entityName' and retrieving the max value for the attribute 'sequenceId'.

        NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *res = [NSEntityDescription entityForName:@"entityName" inManagedObjectContext:managedObjectContext];
    [request setEntity:res];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sequenceId" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release];
    [sortDescriptor release];
    
    [request setFetchLimit:1];
    
    NSError *error = nil;
    NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
    [request release];
    if (results == nil) {
        NSLog(@"error fetching the results: %@",error);
    }
    
    NSInteger maximumValue = 0;
    if (results.count == 1) {
        Result *result = (Result*)[results objectAtIndex:0];
        maximumValue =  [result.sequenceId integerValue];
    }
    
    0 讨论(0)
  • 2020-12-16 15:50

    The issue was that this line is wrong:

    [fetch setResultType:NSDictionaryResultType];  
    

    The result type should have been NSManagedObjectResultType

    Regards

    Darren.

    0 讨论(0)
提交回复
热议问题