2
votes

I am trying to fetch a request and I want to group by "Premium Accounts" and fetch the card number, card holder name and stuff like that. I am getting a weird error -

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'SELECT clauses in queries with GROUP BY components can only contain properties named in the GROUP BY or aggregate functions ((), name gCardHolderName, isOptional 1, isTransient 0, entity Merchant, renamingIdentifier gCardHolderName, validation predicates ( ), warnings ( ), versionHashModifier (null) userInfo { }, attributeType 700 , attributeValueClassName NSString, defaultValue (null) is not in the GROUP BY)'

I assume that's the SQL query - SELECT CARDHOLDERNAME, PREMIUM ACCOUNT NAME FROM "MERCHANT" GROUP BY PREMIUM ACCOUNT NAME.

Premium account name can be the same (for account 1, I can have different card holder names).

I have no clue about this error. Here' my code if anybody can help me out -

pragma mark - FetchRequest Method

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

1 Answers

1
votes

[request setPropertiesToGroupBy:propertiesToFetch]