NSFetchedResultsSectionInfo 对于它有多少个对象与自己不一致

发布于 2024-09-28 06:38:33 字数 1408 浏览 3 评论 0原文

我将其发布在 Apple 开发论坛上,因为我觉得这就像实际 SDK 中的错误,但我想我也将其发布在这里,看看是否有人可以验证我是否错误地使用了这个东西(看起来不像)或者这是错误的行为。

https://devforums.apple.com/thread/72738

--

花了一点时间调试之后在一些代码中,我发现 NSFetchedResultsSectionInfo 实例的行为非常奇怪且令人担忧。

NSFetchedResultsController *frc = [self frcForTableView:tableView];

id <NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:
                                                [indexPath indexAtPosition:1]];

NSLog( @"Looking at %@ with section %@ (%d objects)",
       indexPath, [sectionInfo objects], [sectionInfo numberOfObjects] );

基本上,我抓取一个 FRC,然后从中取出一个sectionInfo 对象(不要担心为什么我抓取索引路径位置 1 而不是 0 ...这里应该不重要)。有趣的是,上面的 NSLog 输出是这样的:

Looking at <NSIndexPath 0x8828ee0> 2 indexes [0, 0] with section (
    "TBN.B x 1 for order 1187",
    "TBN.T x 1 for order 1187"
) (1 objects)`

所以 [sectionInfo objects] 数组中有两个东西,但是 [sectionInfo numberOfObjects] 报告它只有一个。为了消除出现缓存问题的可能性,我在运行此代码之前禁用了 FRC 设置中的所有缓存。

这里很困惑。不知道单个sectionInfo对象如何与它自己有多少个对象不一致。

Apple 开发人员有什么想法吗?运行 XCode 3.2.4 和 4.1 SDK。

编辑:仅供参考,本节的正确对象实际上是第一个对象 (TBN.B),因此在我到目前为止的测试中,如果您只考虑对象数组中最多 的部分numberOfObjects 然后你会得到正确的结果。尽管如此,仍然好奇为什么额外的对象不属于该部分时却出现在数组的末尾。

I posted this on the Apple dev forums here since it feels to me like a bug in the actual SDK, but I thought I'd post it here as well and see if anyone could verify whether or not I'm using this thing incorrectly (doesn't seem like it) or this is broken behaviour.

https://devforums.apple.com/thread/72738

--

After spending a little while debugging some code, I have discovered a very strange and worrying behaviour of an instance of NSFetchedResultsSectionInfo.

NSFetchedResultsController *frc = [self frcForTableView:tableView];

id <NSFetchedResultsSectionInfo> sectionInfo = [[frc sections] objectAtIndex:
                                                [indexPath indexAtPosition:1]];

NSLog( @"Looking at %@ with section %@ (%d objects)",
       indexPath, [sectionInfo objects], [sectionInfo numberOfObjects] );

Basically, I grab a FRC, then pull one of the sectionInfo objects out of it (don't worry about why I'm grabbing for index path position 1 rather than 0 ... it shouldn't matter here). The interesting thing is that the NSLog output for the above is this:

Looking at <NSIndexPath 0x8828ee0> 2 indexes [0, 0] with section (
    "TBN.B x 1 for order 1187",
    "TBN.T x 1 for order 1187"
) (1 objects)`

So the [sectionInfo objects] array has two things in it, but [sectionInfo numberOfObjects] reports that it only has one. To eliminate the possibility of a caching issue I disabled all caching in the FRC setup prior to running this code.

Pretty stumped here. Don't know how a single sectionInfo object can disagree with itself about how many objects it has in it.

Any ideas from the Apple devs? Running XCode 3.2.4 and the 4.1 SDK.

Edit: FYI the correct object for this section is actually the first one (TBN.B) so in my testing so far it appears that if you only consider the portion of the objects array up to numberOfObjects then you get the right results. Nevertheless, still curious as to why the extra object appears at the end of the array when it isn't part of that section.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

欲拥i 2024-10-05 06:38:33

这是问题最初提出后的 5 1/2 年。我们使用的是 Xcode 7.x,该错误似乎仍然存在。有谁知道为什么会发生这种情况。我提出的解决方案只是检查controllerDiDChangeContent 中的这种情况,如果存在,则重新创建FRC。对我来说,没关系,因为只有当您从 0 个部分开始并在其下面添加第二个部分时才会发生这种情况。这是关于如何重现此错误的非常好的文章。我想没有多少人将总计放在页脚中。

https://devforums.apple.com/message/328077#328077

Here it is 5 1/2 years after the question was originally asked. We're on Xcode 7.x and the bug seems to still be here. Has anyone figured out why this is happening. The solution I've come up with is simply to check for this condition in controllerDiDChangeContent and if it exists, recreate the FRC. For me, it's ok since this only happens when you start with 0 sections and add a 2nd one beneath it. Here's a very good writeup on how to recreate this bug. I guess not that many people are putting totals in their footers.

https://devforums.apple.com/message/328077#328077

橙幽之幻 2024-10-05 06:38:33

我的测试表明,第一部分的“对象”数组始终包含控制器中的所有对象。我认为这是一个框架错误。

这是一个非常肮脏(而且缓慢)的解决方法:

NSArray * allObjects = self.cachedFetchedResultsController.fetchedObjects;
NSMutableArray * objectsInSectionZero = [NSMutableArray array];
for (id obj in allObjects) {
    if ([[self.cachedFetchedResultsController indexPathForObject:obj] section] == indexPath.section) {
        [objectsInSectionZero addObject:obj];
    }
}

My tests have shown that the "objects" array of the first section always contains all the objects in the controller. I think thats a framework bug.

Thats a really dirty (and slow) workaround:

NSArray * allObjects = self.cachedFetchedResultsController.fetchedObjects;
NSMutableArray * objectsInSectionZero = [NSMutableArray array];
for (id obj in allObjects) {
    if ([[self.cachedFetchedResultsController indexPathForObject:obj] section] == indexPath.section) {
        [objectsInSectionZero addObject:obj];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文