NSFetchedResultsController 中的部分名称与托管对象值不匹配

2024-05-10

我正在使用 NSFetchedResultsController 用大约 1500 个实体的中等大小的核心数据存储的结果填充 UITableView。结果控制器相当标准 - 一些潜在错误的“热点”并不适合此设置。

  • 在与其使用的同一(主)线程上创建的托管对象上下文
  • 不使用缓存(因为排序经常改变)
  • sectionNameKeyPath 用于将结果拆分为多个部分

然而,我的部分结果非常奇怪。例如,考虑此方法用于设置节标题视图的标题:

- (NSString *)titleForHeaderInSection:(NSInteger)section {
  id <NSFetchedResultsSectionInfo> sectionInfo = [[self.resultsController sections] objectAtIndex:section];
  return [sectionInfo name];    // <------- Stopped at breakpoint here
}

我已使用断点停在指定的行,并使用 GDB 检查了以下内容:

(gdb) po [[self resultsController] sectionNameKeyPath]
reviewDateString
(gdb) print section
$11 = 1
(gdb) print (int) [sectionInfo numberOfObjects]
$12 = 4
(gdb)  po [sectionInfo name]
Wednesday, September 8th 2010
(gdb) po [[[sectionInfo objects] objectAtIndex:0] valueForKey:@"reviewDateString"]
Sunday, February 13th 2011
(gdb) po [[[sectionInfo objects] objectAtIndex:1] valueForKey:@"reviewDateString"]
Sunday, February 13th 2011
(gdb) po [[[sectionInfo objects] objectAtIndex:2] valueForKey:@"reviewDateString"]
Sunday, February 13th 2011
(gdb) po [[[sectionInfo objects] objectAtIndex:3] valueForKey:@"reviewDateString"]
Sunday, February 13th 2011

问题应该很明显 - 为什么 [sectionInfo name] 与该部分中每个托管对象的sectionNameKeyPath 的值不匹配?该部分中的对象似乎已正确分组,只是部分名称设置不正确。

如果你看看这个,结果会更奇怪:

(gdb) po [[self resultsController] indexPathForObject:(id)[[sectionInfo objects] objectAtIndex:0]]
<NSIndexPath 0x6615660> 2 indexes [459, 4294966310]

现在显然,从上面来看,返回的 NSIndexPath 应该是 [1,0],而不是这个伪造的值。

我完全被难住了。有人有主意吗?如果您需要更多信息,我会关注这个问题。

[Edit 1]

我的 NSFetchedResultsController 设置的一件奇怪的事情是,我重新创建(释放并分配/初始化一个新的)结果控制器以响应 UISegmentedControl 的选择更改。这样做是为了更改获取请求的 sortDescriptors 和sectionNameKeyPath,从而使整体排序发生变化。

[Edit 2]

resultsController 方法只是我正在使用的 NSFetchedResultsController 的属性访问器,由 @synthesize 生成。

部分名称的预期值已给出 - 部分名称应等于我在“po [sectionInfo name]”下方显示的 @"reviewDateString" 键(即sectionNameKeyPath)的值,因此对于本例它们应该是“2011 年 2 月 13 日星期日”。


您可能需要在获取时修复您的sectionNameKey。

如果此键路径与 fetchRequest 中的第一个排序描述符指定的路径不同,则它们必须生成相同的相对顺序。例如,fetchRequest 中的第一个排序描述符可能指定持久属性的键; sectionNameKeyPath 可以为从持久属性派生的瞬态属性指定键。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NSFetchedResultsController 中的部分名称与托管对象值不匹配 的相关文章

随机推荐