我正在尝试获取请求,我想通过“高级帐户”分组并获取卡号,持卡人姓名和类似的内容。我收到一个奇怪的错误 -
* 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'具有GROUP BY组件的查询中的SELECT子句只能包含GROUP BY中命名的属性或聚合函数((),名称为gCardHolderName ,isOptional 1,isTransient 0,entity Merchant,renamingIdentifier gCardHolderName,验证谓词( ),警告( ),versionHashModifier(null) 用户信息 { },attributeType 700,attributeValueClassName NSString,defaultValue(null)不在GROUP BY中''
我认为这是SQL查询 - SELECT CARDHOLDERNAME,PREMIUM帐户名称来自“MERCHANT”GROUP PREMIUM帐户名称。
高级帐户名称可以相同(对于帐户1,我可以使用不同的持卡人姓名)。
我对这个错误一无所知。这是'我的代码,如果有人可以帮助我 -
- (void)fetchRequest
{
NSError * anyError = nil;
AppDelegate * applicationDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
NSManagedObjectContext * context = [applicationDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Merchant" inManagedObjectContext:context];
[request setEntity:entity];
[request setFetchBatchSize:15];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"premiumActName" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObjects:sortDescriptor1, nil];
[request setSortDescriptors:descriptors];
NSPropertyDescription *accountDesc = [[entity propertiesByName] objectForKey:@"premiumActName"];
NSPropertyDescription *cardHolderDesc = [[entity propertiesByName] objectForKey:@"gCardHolderName"];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"premiumActName"];
NSExpression *countExpression = [NSExpression expressionForFunction: @"count:"
arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: @"count"];
[expressionDescription setExpression: countExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];
NSArray *propertiesToFetch= @[accountDesc,cardHolderDesc, expressionDescription];
[request setPropertiesToFetch:propertiesToFetch];
[request setPropertiesToGroupBy:[NSArray arrayWithObject:@"premiumActName"]];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
[context save:&anyError];
NSArray * distinctResults = [context executeFetchRequest:request error:&anyError];
[distinctResults enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
NSLog(@" objects=%@", [dict objectForKey:@"gCardHolderName"]);
}];
if(_fetchedResultsController)
{
_fetchedResultsController = nil;
}
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
if(![_fetchedResultsController performFetch:&anyError])
{
NSLog(@"error fetching:%@", anyError);
}
[self.membersTblView reloadData];
}
答案 0 :(得分:1)
[request setPropertiesToGroupBy:propertiesToFetch]