2013-02-15 44 views
2

我正在嘗試獲取請求,並且我想按「高級帳戶」進行分組,並獲取卡號,持卡人姓名以及類似內容。我得到一個奇怪的錯誤 -按核心數據分組給出錯誤

*終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,理由是:「選擇與GROUP查詢子句BY組件只能包含在GROUP BY或聚合函數(命名的屬性() ,名稱gCardHolderName,isOptional 1,isTransient 0,實體商家,renamingIdentifier gCardHolderName,驗證謂詞( ),警告( ),versionHashModifier(空) USERINFO { },屬性類型700,attributeValueClassName的NSString,默認值(空值)不在GROUP BY)'

我假設這是SQL查詢 - SELECT CARD HOLDERNAME,PREMIUM帳戶名稱來自「商人」集團的特惠帳戶名稱。

高級帳戶名稱可以相同(對於帳戶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]; 
} 

回答

1

[請求setPropertiesToGroupBy:propertiesToFetch]