开发者

Section names from NSFetchedResultsController don't match the managed object values

I'm using an NSFetchedResultsController to populate a UITableView with results from a moderately sized Core Data store of ~1500 entities. The results controller is fairly standard - some "hot spots" for potential bugs aren't true of this setup.

  • Managed object context created on the same (main) thread as its used on
  • No cache is used (because the sort changes frequently)
  • A sectionNameKeyPath is used to split the results into sections

My section results, however, are very odd. For example, consider this method used to set开发者_如何学Go the titles for the section header views:

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

I've stopped at the indicated line using a breakpoint and, using GDB, examined the following:

(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

The issue should be evident - why doesn't [sectionInfo name] match the values of the sectionNameKeyPath for each managed object in the section? The objects in the section appear to be properly grouped, the section name just isn't correctly set.

The results are even more odd if you look at this:

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

Now obviously, from the above, the NSIndexPath returned should be [1,0], not this bogus value.

I'm completely stumped. Anyone have any ideas? I'll be watching this question in case you need more information.

[Edit 1]

The one odd thing about my NSFetchedResultsController setup is that I recreate (release and alloc/init a new one) the results controller in response to a UISegmentedControl's selection changing. This is done in order to change the sortDescriptors of the fetch request and sectionNameKeyPath, so that the overall sort changes.

[Edit 2]

The resultsController method is just the property accessor for the NSFetchedResultsController that I'm using, generated by @synthesize.

The expected values for the section names are given already - the section names should be equal to the values for the @"reviewDateString" key (which is the sectionNameKeyPath) that I showed just below "po [sectionInfo name]", so for this case they should be "Sunday, February 13th 2011".


You likely need to fix your sectionNameKey on the fetch.

If this key path is not the same as that specified by the first sort descriptor in fetchRequest, they must generate the same relative orderings. For example, the first sort descriptor in fetchRequest might specify the key for a persistent property; sectionNameKeyPath might specify a key for a transient property derived from the persistent property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜