2

我正在尝试获取一个请求,我想按“高级帐户”分组并获取卡号、持卡人姓名和类似的东西。我收到一个奇怪的错误 -

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“使用 GROUP BY 组件的查询中的 SELECT 子句只能包含在 GROUP BY 或聚合函数中命名的属性 (()、名称 gCardHolderName、isOptional 1、isTransient 0、实体商家、 renamingIdentifier gCardHolderName, 验证谓词 (), 警告 (), versionHashModifier (null) userInfo { }, attributeType 700, attributeValueClassName NSString, defaultValue (null) 不在 GROUP BY)'

我假设这是 SQL 查询 - SELECT CARDHOLDERNAME, PREMIUM ACCOUNT NAME FROM "MERCHANT" GROUP BY PREMIUM ACCOUNT NAME。

高级帐户名称可以相同(对于帐户 1,我可以有不同的持卡人姓名)。

我不知道这个错误。如果有人可以帮助我,这是我的代码 -

杂注标记 - FetchRequest 方法

- (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];
}
4

1 回答 1

1

[请求 setPropertiesToGroupBy:propertiesToFetch]

于 2013-05-09T18:50:38.957 回答