からいくつかの値を取得しCoreData Entity
、結果を取得しました。クエリはうまく機能します。今...私はそれらの結果がどこから来たのか知りたいです。表示されている出力は、生の結果からのものです。どうすればクエリから/を吐き出すことができますか?Averaged
GroupedBy
date
dates
date
GroupBy value
(given that my GroupBy criteria is date)
出力
(
{
averageWeight = 36;
},
{
averageWeight = 22;
}
)
コード:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ExerciseData" 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:@"weight"];
// Create an expression to represent the function you want to apply
NSExpression *expression = [NSExpression expressionForFunction:@"average:"
arguments:@[keyPathExpression]];
// Create an expression description using the minExpression and returning a date.
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
// The name is the key that will be used in the dictionary for the return value.
[expressionDescription setName:@"averageWeight"];
[expressionDescription setExpression:expression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType]; // For example, NSDateAttributeType
// Set the request's properties to fetch just the property represented by the expressions.
[request setPropertiesToFetch:@[expressionDescription]];
request.predicate = [NSPredicate predicateWithFormat:@"exercise == %@",exercise];
[request setPropertiesToGroupBy:[NSArray arrayWithObject:@"date"]];
// Execute the fetch.
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
NSLog(@"%@",objects);