导致此iOS崩溃的原因是什么? UICollectionView接收具有不存在的索引路径的单元格的布局属性 [英] What's causing this iOS crash? UICollectionView received layout attributes for a cell with an index path that does not exist

查看:2874
本文介绍了导致此iOS崩溃的原因是什么? UICollectionView接收具有不存在的索引路径的单元格的布局属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款具有UICollectionViewController的应用程序,该应用程序在某些难以重现的神秘情况下崩溃。崩溃的日志如下所示:

I'm working on an app that has a UICollectionViewController that is crashing in certain mysterious situations that are hard to reproduce. The log for the crash looks like this:

*** Assertion failure in -[UICollectionViewData validateLayoutInRect:], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UICollectionViewData.m:417
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000008016> {length = 2, path = 0 - 1}'

这样的崩溃似乎只是开始发生在我们的代码中,一旦我们切换到iOS 8 SDK。

Crashes like this only seem to have started occurring in our code once we switched to the iOS 8 SDK.

为什么会发生这种情况?

Why is this happening?

注意:我已经知道问题的答案是什么,但我发现很少有关于Stack Overflow和网络其他部分崩溃的信息。我将在下面发布答案。这个错误让我的同事和我三天追踪,所以希望这篇文章能为其他人节省很多时间和挫折。我已经向Apple提交了这个错误。

Note: I already know what the answer to question is, but I found very little information relating to this crash on Stack Overflow and the rest of the web. I'll post the answer below. This bug took my coworker and I three days to track down, so hopefully this post will save someone else a lot of time and frustration. I have filed this bug with Apple.

推荐答案

崩溃发生在以下情况:

我们有一个集合视图控制器,它在它上面显示另一个视图控制器。

We had a collection view controller that was presenting another view controller on top of it.

当集合视图控制器不再可见时,我们的应用程序的后端请求偶尔会发生以下事件序列。

While the collection view controller was no longer visible, the following sequence of events was occasionally occurring in response to our app's back end requests.


  1. [UICollectionView insertItemsAtIndexPaths:] 在隐藏的 UICollectionViewController 的集合视图中使用50
    项目进行调用。

  2. <$隐藏的集合视图上调用了c $ c> [UICollectionView reloadData] 。

  3. 会发生短暂的延迟。

  4. 隐藏集合视图中的项目数设置为较小的数字。

  5. [UICollectionView reloadData] 再次被调用。

  6. 视图控制器被解雇,显示隐藏den集合视图控制器。

  1. [UICollectionView insertItemsAtIndexPaths:] was called with 50 items on the collection view of the hidden UICollectionViewController.
  2. [UICollectionView reloadData] was called on the hidden collection view.
  3. A short delay would occur.
  4. The number of items in the hidden collection view was set to a small number.
  5. [UICollectionView reloadData] was called again.
  6. The view controller was dismissed, revealing the hidden collection view controller.

内部UIKit类中的断言失败 UICollectionViewData 将在第6步发生。

The assertion failure in the internal UIKit class UICollectionViewData would happen at step 6.

所以,教训是,尽量避免操纵屏幕上看不到的集合视图。

So, the lesson is, try to avoid manipulating a collection view that is not visible on the screen.

我们解决此问题的方法是调用 [UICollectionView reloadSections:] 而不是 [UICollectionView reloadData] 在关键点。

Our workaround for this problem was to call [UICollectionView reloadSections:] instead of [UICollectionView reloadData] at key points.

我们怀疑 reloadData 的影响将在未来的某个时间推迟,因此,这可能与其他方法调用(如 insertItemsAtIndexPaths )之间的交互方式存在细微问题,而 reloadSections 会立即处理,将集合视图保持在更好的状态。

We suspect that the effects of reloadData are deferred to some point in the future, and there are consequently subtle issues with how this may interact with other method calls like insertItemsAtIndexPaths, whereas reloadSections is handled immediately, leaving the collection view in a better state.

我们认为在我们开始为iOS 8构建应用程序之前,我们没有看到这种行为。

We think that we were not seeing this behavior until we started building our app for iOS 8.

好好睡觉,我的朋友们!

Sleep well, my friends!

这篇关于导致此iOS崩溃的原因是什么? UICollectionView接收具有不存在的索引路径的单元格的布局属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆