2013-06-12 46 views
5

我有一个提取请求,从结果中的两个部分产生tableview。核心数据NSFetchRequest与分组

我的模型:

@interface Player : NSManagedObject 

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

的获取这种模式要求应导致与人是isFacebookFriend节==是在一个部分,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因为禁用自动更新通知。