2013-06-12 2 views
5

결과에서 두 섹션이있는 테이블 뷰가 생성되는 페치 요청이 있습니다.핵심 데이터 NSFetchRequest with grouping

내 모델 :

@interface Player : NSManagedObject 

@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSNumber * isFacebookFriend; 

가 isFacebookFriend 사람들과 단면이 발생한다 모델 요청 페치 == YES 일 개 섹션 및 isFacebookFriend == NO 번째 부분이다.

나는

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
HBAppDelegate *appDelegate = [UIApplication sharedApplication].delegate; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:appDelegate.managedObjectContext]; 
[fetchRequest setEntity:entity]; 
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 
[fetchRequest setSortDescriptors:@[nameDescriptor]]; 
[fetchRequest setResultType:NSDictionaryResultType]; 
[fetchRequest setPropertiesToGroupBy:@[@"isFacebookFriend"]]; 
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:@"playerCache"]; 
NSError *error; 
[_fetchedResultsController performFetch:&error]; 

과 노력하지만 그것을하지 않았다. 오류가 있었다 :

2013-06-12 12:27:28.364 TwentyQuestions[25015:c07] Unbalanced calls to begin/end appearance transitions for <UINavigationController: 0xb676aa0>. 
2013-06-12 12:27:31.119 TwentyQuestions[25015:c07] *** 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 ((<NSAttributeDescription: 0xc382320>), name hasRegisteredForGame, isOptional 1, isTransient 0, entity Player, renamingIdentifier hasRegisteredForGame, validation predicates (
), warnings (
), versionHashModifier (null) 
userInfo { 
}, attributeType 800 , attributeValueClassName NSNumber, defaultValue (null) is not in the GROUP BY)' 
+0

전체 가져 오기 요청 코드를 표시 하시겠습니까? –

+0

추가했습니다. 핵심 데이터에서 표준 가져 오기 만하면됩니다. – hakonbogen

답변

9

이 섹션 테이블보기를 만들려면 sectionNameKeyPath 매개 변수 NSFetchedResultsController의 을 사용해야합니다. 또한 섹션 이름 키 경로에 따라 을 정렬하는 (첫 번째) 정렬 설명자를 추가해야합니다.

NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 
NSSortDescriptor *isFacebookFriendDescriptor = [[NSSortDescriptor alloc] initWithKey:@"isFacebookFriend" ascending:NO]; 
[fetchRequest setSortDescriptors:@[isFacebookFriendDescriptor, nameDescriptor]]; 
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
      managedObjectContext:appDelegate.managedObjectContext 
       sectionNameKeyPath:@"isFacebookFriend" 
         cacheName:@"playerCache"]; 

당신은 setPropertiesToGroupBy을 설정할 필요가 없습니다, 그리고이 자동 업데이트 알림을 비활성화 때문에 NSDictionaryResultType을 설정하는 것을 권장하지 않습니다.