リクエストを取得しようとしていますが、「プレミアムアカウント」でグループ化し、カード番号、カード所有者名などを取得したいと考えています。私は奇妙なエラーが発生しています -
*キャッチされない例外 'NSInvalidArgumentException' が原因でアプリを終了します。理由: 'GROUP BY コンポーネントを含むクエリの SELECT 句には、GROUP BY または集計関数 (()、名前 gCardHolderName、isOptional 1、isTransient 0、エンティティ Merchant で指定されたプロパティのみを含めることができます。 renamingIdentifier gCardHolderName, validation predicates ( ), warnings ( ), 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];
}