在我的 ios 项目中,我使用了两个实体(CoreData):Person 和 Gifts with To-Many 关系
我知道如何计算给一个人的礼物总和:
NSDecimalNumber *orderSum=[person.gifts valueForKeyPath:@"@sum.priceOfGift"];
但是我如何计算所有人的总和?
-(NSFetchedResultsController *) fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
//NSError *error = nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person"
inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"nameOfPerson"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
_fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"nameOfGroup" cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}