问题描述
我的应用有两个 CollectionViewController
.在给定时间只有一个可见.
My app has two CollectionViewController
s. Only one is visible at a given time.
我在情节提要上创建了以下结构:两个容器视图相互叠加.每个容器视图都嵌入了一个 CollectionViewController
.特定容器视图的可见性决定了哪个 collectionViewController 可见.
I have created the following structure on storyboard: two container views on top of each other. Every container view has a CollectionViewController
embedded. The visibility of a particular container view determines which collectionViewController is visible.
这就是问题所在.两个 CollectionViewController
都在并行接收数据,但是 iOS 有一个错误,如果一个 CollectionViewController 在不可见的情况下尝试使用 performBatchUpdates
执行插入,则会导致应用崩溃.
This is the problem. Both CollectionViewController
s are receiving data in parallel but iOS has a bug that will make the app crash if one CollectionViewController tries to execute an insert using performBatchUpdates
while it is invisible.
为了防止这种情况,我在两个 CollectionViewController
上都创建了一个 BOOL 标志,这样他们就可以知道它们是否可见并执行 performBatchUpdates
.比如:
Trying to prevent that, I have created a BOOL flag on both CollectionViewController
s so they can know if they are visible and execute or not the performBatchUpdates
. Something like:
if (self.isThisCollectionViewVisible == NO) return;
[self.collectionView performBatchUpdates:^{
// bla bla... perform insert,m remove...
这解决了部分问题.但是应用程序在以下情况下继续崩溃:如果我点击按钮切换到不可见的 CollectionViewController
使其在接收更新时可见.
This solves part of the problem. But the app continues to crash on the following condition: if I tap the button to switch to the invisible CollectionViewController
making it visible while it is receiving updates.
我的意思是:让 A
第一个 CollectionViewController
和 B
第二个.A
是可见的,而 B
在这一点上是不可见的.B
开始接收数据并尝试执行 performBatchUpdates
但由于它不可见,因此 if (self.isThisCollectionViewVisible == NO) 返回;
正在阻止 performBatchUpdates
运行,这很好.现在我使 A
不可见,而 B
可见.此时标志 self.isThisCollectionViewVisible
设置为 YES
并且 performBatchUpdates
使应用程序崩溃并出现以下错误:
I mean this: lets call A
the first CollectionViewController
and B
the second one. A
is visible and B
is invisible at this point. B
starts receiving data and is trying to do a performBatchUpdates
but as it is invisible, the if (self.isThisCollectionViewVisible == NO) return;
is preventing performBatchUpdates
to run, what is fine. Now I make A
invisible and B
visible. At this point the flag self.isThisCollectionViewVisible
is set to YES
and performBatchUpdates
makes the app crash with this error:
* -[CollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:] 中的断言失败,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UICollectionView.m:4625* 由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:无效更新:无效第 0 节中的项目数.包含在第 0 节中的项目数更新后的现有节(76)必须等于更新前该部分中包含的项目 (70),加号或减号从该部分插入或删除的项目数(插入 5 个,2 已删除)和加减移入或移出的项目数该部分(0 移入,0 移出).'
我认为 CollectionViewController
确实还没有准备好并更新到能够执行 performBatchUpdates
... 这不是不更新数据源的问题以前是因为它正在更新.
I think the CollectionViewController
is really not yet ready and updated to be able to do a performBatchUpdates
... and this is not a matter of not updating the data source previously because it is being updated.
我可以做哪些检查来防止这种情况发生?
What checks can I do to prevent that from happening?
注意:我特别注意到这次崩溃有一些奇怪的地方.它说正在插入 5 个元素并删除了 2 个元素,但实际上当崩溃发生时正在插入 3 个元素,删除了 0 个元素并更改了 2 个元素.
NOTE: I noticed something strange about this crash in particular. It says that 5 elements are being inserted and 2 deleted but in fact 3 elements are being inserted, 0 deleted and 2 changed when the crashes happen.
推荐答案
为我添加 self.collectionView.numberOfItemsInSection(0)
修复了崩溃.collectionView 在不可见时插入项目时出现问题.
For me adding self.collectionView.numberOfItemsInSection(0)
fixed the crash.The collectionView has issues while inserting items when it is not visible.
似乎我的解决方案并不孤单:http://www.openradar.me/15262692一个>
Seems like I'm not alone with my solution: http://www.openradar.me/15262692
这篇关于如何解决此 CollectionView 崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!