【问题标题】:Group by Core Data gives error按核心数据分组给出错误
【发布时间】:2013-01-30 22:37:10
【问题描述】:

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

* 由于未捕获的异常“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 ACCOUNT NAME FROM "MERCHANT" GROUP BY PREMIUM ACCOUNT NAME。

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

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

pragma mark - 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];
}

【问题讨论】:

    标签: core-data nsfetchedresultscontroller nsfetchrequest


    【解决方案1】:

    [请求 setPropertiesToGroupBy:propertiesToFetch]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 2017-07-20
      相关资源
      最近更新 更多