【问题标题】:Group of sections not consistent when using NSFetchedResultsController使用 NSFetchedResultsController 时的部分组不一致
【发布时间】:2009-10-17 22:47:31
【问题描述】:

我正在使用一个 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"];                   

我的对象模型与其他问题中概述的相同:

https://stackoverflow.com/questions/1580236/how-to-setup-a-predicate-for-this-query 有没有办法确保我每次都得到一致的分组?

【问题讨论】:

    标签: iphone core-data


    【解决方案1】:

    原来这么简单:

    NSFetchedResultsController *controller = 
      [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
          managedObjectContext:context 
          sectionNameKeyPath:@"groups.name" 
          cacheName:@"Root"];
    

    我没有意识到我可以在“sectionNameKeyPath”中附加二级属性名称

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 2010-11-21
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多