如何解决这个CollectionView崩溃? [英] How to solve this CollectionView crash?

查看:1039
本文介绍了如何解决这个CollectionView崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用有两个 CollectionViewController s。在给定时间只能看到一个。

My app has two CollectionViewControllers. 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 c>执行插入操作,将导致应用程序崩溃code>虽然它是不可见的。

This is the problem. Both CollectionViewControllers 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标志 s所以他们可以知道它们是否可见并执行或不执行 performBatchUpdates 。类似于:

Trying to prevent that, I have created a BOOL flag on both CollectionViewControllers 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 ,但由于它不可见,如果(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部分中的项目数。更新后
现有部分中包含的项目数(76)必须等于该部分中包含的
项目数更新(70),加上或减去
从该部分插入或删除的项目数(插入5个,删除
2)并加上或减去移入或移出$ b $的项目数b该部分(0移入,0移出)。'

* Assertion failure in -[CollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UICollectionView.m:4625 * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (76) must be equal to the number of items contained in that section before the update (70), plus or minus the number of items inserted or deleted from that section (5 inserted, 2 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

我认为 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崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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