【问题标题】:Sum of all values of a column in coredatacoredata 中某一列的所有值的总和
【发布时间】:2014-03-14 07:15:04
【问题描述】:

我正在尝试将我的 NSFetchRequest 设置为核心数据以检索列的所有值的总和。我的学生记录采用以下格式

 name  | id  |   marks |
_______|_____|_________|
Jack   |  12 |    34   |
John   |  13 |    27   |
Jeff   |   1 |    42   |
Don    |  34 |    32   |
Edward |  43 |    35   |
Ricky  |  23 |    24   |

谁能建议我设置一个 NSFetchRequest 来返回记录中所有标记的总和

【问题讨论】:

    标签: ios objective-c core-data sum


    【解决方案1】:

    NSExpressions 会帮助你。

    NSManagedObjectContext *context = …your context;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student"
                                              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:@"marks"];
    
    // Create an expression to represent the sum of marks
    NSExpression *maxExpression = [NSExpression expressionForFunction:@"sum:"
                                                            arguments:@[keyPathExpression]];
    
    NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
    [expressionDescription setName:@"marksSum"];
    [expressionDescription setExpression:maxExpression];
    [expressionDescription setExpressionResultType:NSInteger32AttributeType];
    
    // 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 *result = [context executeFetchRequest:request error:&error];
    
    NSLog(@"%@", result);
    

    【讨论】:

      【解决方案2】:

      为什么NSExpression 是一个简单的总和??

      NSInteger sum = [allRecords valueForKeyPath:@"@sum.marks"].integerValue;
      

      【讨论】:

      • 你的意思是先加载内存中的所有记录?
      • 是的,但是Core Data不会将所有数据加载到内存中,只是一个指针数组。因此,资源压力并不显着。试试看 - 它非常有效。这个解决方案远远优于NSExpression 版本。
      • 如果我也使用 group by,如何将此解决方案分配给获取的结果控制器
      【解决方案3】:

      您必须使用 CoreData 聚合函数。

      NSExpression *keyExpression = [NSExpression expressionForKeyPath:@"marks"];
      
      //create the NSExpression to tell our NSExpressionDescription which calculation we are performing.
      NSExpression *maxExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyExpression]];
      
      NSExpressionDescription *description = [[NSExpressionDescription alloc] init];
      [description setName:@"markSum"];
      [description setExpression:maxExpression];
      [description setExpressionResultType:NSInteger32AttributeType];
      
      [request setPropertiesToFetch:[NSArray arrayWithObject:description]];
      
      NSArray *results = [context executeFetchRequest:request error:&error];
      if (results != nil && results.count > 0){
          NSNumber *markSum = [[results objectAtIndex:0] valueForKey:@"markSum"];
          NSLog(@"Sum: %@", markSum);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-20
        • 2017-07-03
        • 2017-03-30
        • 1970-01-01
        • 1970-01-01
        • 2018-07-12
        • 1970-01-01
        相关资源
        最近更新 更多