Rufen Sie aggregierte Daten aus NSManagedObject ab, indem Sie einen anderen Ausdruck als Argument für sum: expression verwenden
Ist es möglich, Ausdruck als Argument für zu verwenden?sum:
Ausdruck?
Ich habe Entität (NSManagedObject
Klasse)Drink
mit EigenschaftencostPerDrink
, numberOfDrinks
unddrinkingDate
.
Ich möchte die Summe der Gesamtkosten erhalten (numberOfDrinks
multipliziert mitcostPerDrink
) innerhalb des Zeitraums. Ich habe zwar keine Probleme, die Summe für ein einzelnes Grundstück zu erhalten (z. B. die Summe vonnumberOfDrinks
Wenn ich versuche, den Summenausdruck für einen anderen (Multiplikations-) Ausdruck zu verwenden, erhalte ich folgende Fehlermeldung:
NSInvalidArgumentException, reason: Unsupported argument to sum :
(
"costPerDrink * numberOfDrinks"
)
Siehe Code:
// Init fech
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Drink" inManagedObjectContext:[Service appContext]];
[request setEntity:entity];
// Return dictionary
[request setResultType:NSDictionaryResultType];
// Set conditions
[request setPredicate:[NSPredicate predicateWithFormat:@"(drinkingDate>=%@) AND (drinkingDate<=%@)", self.startDate, self.endDate]];
// Set 1st column to be returned - count of drinks
NSExpression *drinksExpression = [NSExpression expressionForKeyPath:@"numberOfDrinks"];
NSExpression *sumDrinksExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:drinksExpression]];
NSExpressionDescription *sumDrinksResultDescription = [[NSExpressionDescription alloc] init];
[sumDrinksResultDescription setName:@"sumDrinks"];
[sumDrinksResultDescription setExpression:sumDrinksExpression];
[sumDrinksResultDescription setExpressionResultType:NSInteger32AttributeType];
// Set 2nd column to be returned - total cost
NSExpression *costExpression = [NSExpression expressionForKeyPath:@"costPerDrink"];
NSExpression *totalExpression = [NSExpression expressionForFunction:@"multiply:by:"
arguments:[NSArray arrayWithObjects:costExpression,
drinksExpression, nil]];
NSExpression *sumCostExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:totalExpression]];
NSExpressionDescription *sumCostResultDescription = [[NSExpressionDescription alloc] init];
[sumCostResultDescription setName:@"sumCost"];
[sumCostResultDescription setExpression:sumCostExpression];
[sumCostResultDescription setExpressionResultType:NSDecimalAttributeType];
// Add expressions to fetch
[request setPropertiesToFetch:[NSArray arrayWithObjects: sumDrinksResultDescription, sumCostResultDescription, nil]];
// Execute fech
NSError *error;
NSArray *result = [[Service appContext] executeFetchRequest:request error:&error];
Ich verstehe sehr gut, dass ein bestimmtes Problem durch Hinzufügen von automatisch berechneten gelöst werden kanntotalCost
Eigentum anDrink
Entität und dann mitsum:
in holen ohnemultiply:by:
Ausdruck, aber Frage bleibt - ist es möglich, Ausdruck als Argument für zu verwendensum:
Ausdruck?