Ios coredata中一列的所有值之和
我正在尝试设置对核心数据的NSFetchRequest,以检索列的所有值之和Ios coredata中一列的所有值之和,ios,objective-c,core-data,sum,Ios,Objective C,Core Data,Sum,我正在尝试设置对核心数据的NSFetchRequest,以检索列的所有值之和 name | id | marks | _______|_____|_________| Jack | 12 | 34 | John | 13 | 27 | Jeff | 1 | 42 | Don | 34 | 32 | Edward | 43 | 35 | Ricky | 23 | 24 | 任何人都可以建议我设
name | id | marks |
_______|_____|_________|
Jack | 12 | 34 |
John | 13 | 27 |
Jeff | 1 | 42 |
Don | 34 | 32 |
Edward | 43 | 35 |
Ricky | 23 | 24 |
任何人都可以建议我设置一个NSFetchRequest,返回记录中所有标记的总和您必须使用CoreData agregate函数
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);
}
您必须使用coredataagregate函数
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);
}
N表达式将帮助您
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);
N表达式将帮助您
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);
为什么要将N表达式用于简单求和
NSInteger sum = [allRecords valueForKeyPath:@"@sum.marks"].integerValue;
为什么要将N表达式用于简单求和
NSInteger sum = [allRecords valueForKeyPath:@"@sum.marks"].integerValue;
你的意思是首先在内存中加载所有记录?是的,但核心数据并没有将所有数据加载到内存中,只是一个指针数组。因此,对资源的压力并不显著。试试看,它非常有效。此解决方案远远优于NSExpression版本。如果我也使用group By,如何将此解决方案分配给获取的结果控制器?您的意思是首先将所有记录加载到内存中?是的,但核心数据不会将所有数据加载到内存中,只是一个指针数组。因此,对资源的压力并不显著。试试看,它非常有效。此解决方案远远优于NSExpression版本。如果我同时使用group by,如何将此解决方案分配给获取的结果控制器