我试图以其最基本的形式使用组核心数据功能:
- (NSFetchedResultsController *)fetchedResultsController {
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSExpression *reviewNumKeyExpression = [NSExpression expressionForKeyPath:@"review_num"];
NSExpression *reviewMaxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:reviewNumKeyExpression]];
NSExpressionDescription *maxReviewED = [[NSExpressionDescription alloc] init];
[maxReviewED setName:@"maxReview"];
[maxReviewED setExpression:reviewMaxExpression];
[maxReviewED setExpressionResultType:NSInteger32AttributeType];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Group" inManagedObjectContext:managedObjectContext];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setEntity:entity];
NSAttributeDescription *groupId = [entity.propertiesByName objectForKey:@"group_id"];
NSArray *propertiesToFetch = [NSArray arrayWithObjects:groupId, maxReviewED, nil];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[fetchRequest setPropertiesToGroupBy:propertiesToFetch];
...在运行此代码时,我会得到以下错误:
由于未命名的异常“NSInvalidArgumentException”终止应用程序,原因:“无效的键盘表达式( )、名称maxReview、isOptional 1、isTransient 0、实体(null)、renamingIdentifier maxDepartment、验证谓词( )、警告()、versionHashModifier (null) userInfo { })传递给setPropertiesToFetch
我想我错过了一些API,所以任何帮助都会非常感谢!
发布于 2016-02-08 14:30:01
我认为错误信息是错误的:问题在于setPropertiesToGroupBy,而不是setPropertiesToFetch。您不能按计算值(maxReview)进行分组,但我怀疑您是否真的想这样做:如果您想要每个groupId的review_num最大值,则只需要propertiesToGroupBy中的groupId。
NSAttributeDescription *groupId = [entity.propertiesByName objectForKey:@"group_id"];
NSArray *propertiesToFetch = [NSArray arrayWithObjects:groupId, maxReviewED, nil];
NSArray *propertiesToGroupBy = [NSArray arrayWithObjects:groupId, nil];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[fetchRequest setPropertiesToGroupBy:propertiesToGroupBy];https://stackoverflow.com/questions/35263730
复制相似问题