3

In my ios project I use two entities (CoreData): Person and Gifts with To-Many Relationship

I know how to calculate the sum of gifts to one person:

NSDecimalNumber *orderSum=[person.gifts valueForKeyPath:@"@sum.priceOfGift"];

But how do I calculate the sum of all persons?

 -(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;

}

German
  • 137
  • 17

2 Answers2

2

The "sum of all persons" would be to fetch all persons and count them

int sum = fetchedObjects.count;

But perhaps you mean the "sum of the prices of all gifts of all persons". If you think about it, it is the same as the sum of the prices of all gifts. Thus, you can just fetch the gifts and calculate

NSNumber *sum = [self.fetchedResultsController.fetchedObjects 
      valueForKeyPath:@"@sum.priceOfGift"];
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Dear Mundi thanks for your response. Please take a look at the updated code, how i can use : NSNumber *sum = [fetchedObjects valueForKeyPath:@"@sum.priceOfGift"]; Thank you – German Dec 20 '12 at 12:55
  • `fetchedObjects` is a property of `NSFetchedResultsController`. See above. – Mundi Dec 20 '12 at 17:53
  • NSDecimalNumber *sum = [self.fetchedResultsController.fetchedObjects valueForKeyPath:@"@sum.priceOfGift"]; totalLabel.text = [currencyFormatter stringFromNumber:sum]; NSLog(@"%@ Sum ", sum); In the console, I get a (null) Sum – German Dec 20 '12 at 18:54
  • Go ahead and debug. Is price of gift NSNumber? No spelling mistakes? Double check the managed object subclass declarations, etc. – Mundi Dec 20 '12 at 18:59
  • price of gift - NSDecimalNumber. No Spelling mistakes. Sorry but what exactly should I check the managed object subclass declarations? – German Dec 20 '12 at 19:16
  • 1
    The attribute should be of type NSNumber. In .m file it should be `@dynamic`. You should verify that there are valid numbers to be summed in the database (e.g. via NSLog). – Mundi Dec 20 '12 at 19:32
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21475/discussion-between-astakhoff-and-mundi) – German Dec 20 '12 at 19:36
1

You can try to fetch all the Persons, then use the sum

    NSError *error = nil;
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext];
    request.predicate = nil;
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"index" ascending:YES]];//some parameter
    NSArray *persons =  [managedObjectContext executeFetchRequest:request error:&error];
 NSDecimalNumber *orderSum;
 for (Person *person in persons)
  orderSum = [NSDecimalNumber decimalNumberWithDecimal:orderSum.decimalValue
+[person valueForKeyPath:@"gifts.@sum.priceOfGift"].decimalValue]

Though, now work with NSDecimalNumber look a bit uncomfortable.

Nikita Pestrov
  • 5,876
  • 4
  • 31
  • 66