Iphone 使用NSFetchedResultsController时节组不一致

Iphone 使用NSFetchedResultsController时节组不一致,iphone,core-data,Iphone,Core Data,我正在使用一个NSFetchedResultsController,它的fetchRequest有一个谓词。然而,每次执行查询时,它似乎并没有给我一致的分组 我已经为NSFetchedResultsController设置了“sectionNameKeyPath”,并根据在运行获取之前是否使用根对象返回了不同数量的节。有时我得到3个部分,其他时间,它返回1个部分,这是预期的结果 如何创建FetchRequestController: // Create the fetch request for

我正在使用一个NSFetchedResultsController,它的fetchRequest有一个谓词。然而,每次执行查询时,它似乎并没有给我一致的分组

我已经为NSFetchedResultsController设置了“sectionNameKeyPath”,并根据在运行获取之前是否使用根对象返回了不同数量的节。有时我得到3个部分,其他时间,它返回1个部分,这是预期的结果

如何创建FetchRequestController:

// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

// Configure the request's entity and its predicate.        
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];

// The predicate to find all employees associated with a Group
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.groups IN %@",
                         [division groups]];

[fetchRequest setPredicate:predicate];

// Sort based on create date and time
NSSortDescriptor *createDateSortDcptor = [[NSSortDescriptor alloc] initWithKey:@"createDateTime" ascending:YES];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:createDateSortDcptor, nil];

[fetchRequest setSortDescriptors:sortDescriptors];

// should be grouped by the 'Group' employee belongs to.
NSFetchedResultsController *controller = 
  [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
      managedObjectContext:context 
      sectionNameKeyPath:@"groups" 
      cacheName:@"Root"];                   
我的对象模型与另一个问题中概述的相同:


有没有办法确保我每次都能得到一致的分组?

事实证明,这样做很简单:

NSFetchedResultsController *controller = 
  [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
      managedObjectContext:context 
      sectionNameKeyPath:@"groups.name" 
      cacheName:@"Root"];
我没有意识到我可以在“sectionNameKeyPath”中附加二级属性名