【发布时间】:2011-06-28 00:52:39
【问题描述】:
我正在使用 NSFetchedResultsController 来填充 UITableView,其中包含来自约 1500 个实体的中等大小的核心数据存储区的结果。结果控制器是相当标准的 - 一些潜在错误的“热点”不适用于此设置。
- 在与其使用的相同(主)线程上创建的托管对象上下文
- 没有使用缓存(因为排序经常变化)
- 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],而不是这个假值。
我完全被难住了。谁有想法?如果您需要更多信息,我会关注这个问题。
[编辑 1]
关于我的 NSFetchedResultsController 设置的一件奇怪的事情是,我重新创建(释放并分配/初始化一个新的)结果控制器以响应 UISegmentedControl 的选择更改。这样做是为了更改 fetch 请求的 sortDescriptors 和 sectionNameKeyPath,从而使整体排序发生变化。
[编辑 2]
resultsController 方法只是我正在使用的 NSFetchedResultsController 的属性访问器,由 @synthesize 生成。
已经给出了部分名称的预期值 - 部分名称应该等于我在“po [sectionInfo name]”下方显示的@“reviewDateString”键(即sectionNameKeyPath)的值,所以在这种情况下,它们应该是“2011 年 2 月 13 日,星期日”。
【问题讨论】:
-
你能发布 fetchResultController 方法吗?
-
+1 个有趣的问题,你已经完成了作业。如上所述,我们可以看看您的
fetchedResultsController方法和您的部分名称的实际 x 预期值吗?
标签: iphone cocoa-touch ios core-data nsfetchedresultscontroller