嗨,我希望能够在取到的结果控制器中已经有一个谓词过滤后,在我的CoreData数据库中求和一个属性(rowTotal)。结果将是订单的小计。

最初,我认为我可以以某种方式对tableCells中显示的值求和,但是我已经读到它不能很好地工作,因为100个项目可能只有10个tableCells。

我已经阅读了一个类似的问题(下面的链接),该问题涉及将总和放入节头中。我想要一种变体,希望在对ManagedObjectContext进行任何更改后将总和显示并更新到UILabel。

Sum a CoreData attribute

我尝试了以下代码:

float subtotal = 0;
for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects]) {
NSNumber *objectRowTotalNumber = [object valueForKey:@"rowTotal"];
float objectRowTotal = [objectRowTotalNumber floatValue];
subtotal = subtotal + objectRowTotal;
}
NSLog(@"Subtotal: %f", subtotal);


我已经在代码中的多个位置进行了尝试,以查看是否可以正常工作。我没有成功,并且此代码的​​数据未显示在tableView中。我认为以下内容使我感到悲伤。

for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects])


我感觉到它实际上在创建一个新的托管对象并干扰了我已有的对象。

这是我的获取结果控制器的代码

- (NSFetchedResultsController *)fetchedResultsController {
NSLog(@"I'm inside the method fetchedResultsController");


/*
 Set up the fetched results controller.
 */
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
OrderAppDelegate *appDelegate =
(OrderAppDelegate*) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Order" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

// Set the Predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"orderNumber == %@", orderNumberLabel.text];
NSLog(@"predicate is: %@",predicate);
[fetchRequest setPredicate:predicate];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;


[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor release];
[sortDescriptors release];

return fetchedResultsController;
}


我是否为此应用使用正确的代码?你能看到我做错了什么吗?
我应该在哪里实施代码?

任何指导将不胜感激,谢谢。

最佳答案

顺便说一句,您可以更改此:

float subtotal = 0;
for (NSManagedObject *object in [self.fetchedResultsController fetchedObjects]) {
    NSNumber *objectRowTotalNumber = [object valueForKey:@"rowTotal"];
    float objectRowTotal = [objectRowTotalNumber floatValue];
    subtotal = subtotal + objectRowTotal;
}
NSLog(@"Subtotal: %f", subtotal);


对此:

CGFloat subtotal = [[[[self fetchedResultsController] fetchedObjects] valueForKeyPath:@"@sum.rowTotal"] floatValue]

07-24 18:58
查看更多