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

Bry*_*nry 3 iphone cocoa-touch core-data nsfetchedresultscontroller ios

我正在使用NSFetchedResultsController来填充UITableView,其结果来自大约1500个实体的中等大小的Core Data存储.结果控制器是相当标准的 - 潜在错误的一些"热点"不适用于此设置.

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

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

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

我使用断点停在指定的行,并使用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
Run Code Online (Sandbox Code Playgroud)

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

如果你看一下这个结果就更奇怪了:

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

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

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

[编辑1]

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

[编辑2]

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

部分名称的预期值已经给出 - 部分名称应该等于我在"po [sectionInfo name]"下面显示的@"reviewDateString"键(这是sectionNameKeyPath)的值,因此对于这种情况他们应该是"2011年2月13日星期日".

Jos*_*erg 10

您可能需要在fetch上修复sectionNameKey.

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