记一次崩溃记录: collectionView 的 reloadSection 意外崩溃

崩溃记录

'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (7) must be equal to the number of sections contained in the collection view before the update (9), plus or minus the number of sections inserted or deleted (1 inserted, 1 deleted).'复制代码

 问题发现. 在collectionview 中有个需求须要点击 刷新某个单独的 section , 出于性能的考虑,选择了

`self.collectionView.reloadSections(IndexSet(integer:sender.tag))`
这个方法, 可是后台监听发现有 carsh ,思索以后发现,reloadSections不单单是刷新当前行, 还会调用当前行以前和以后的 section 数据, 当发生数据不一致时候,就会发生 carsh

解决方案

google 一番以后 , 发现广泛推荐这个解决方案,
``` collectionView.performBatchUpdates({ UIView.performWithoutAnimation { self.collectionView.reloadSections(IndexSet(integer:sender.tag)) } }) { (success) in } ```

使用这个方法, 能够 collectionView 暂时锁定 collectionView 的数据,相似 tableview 的 beginData, 或者 enddata 方法缓存

手动试了一下 异步删除数据,在performBatchUpdates 方法中 刷新section, 发现仍是会报出崩溃
再见👋, performBatchUpdates
这里推荐使用这种,
 self.collectionView.reloadData() self.collectionView.collectionViewLayout.invalidateLayout()

核心方法是这个 invalidateLayout()bash

将collectionViewLayout 的缓存信息销毁, 从新生成 layout异步

这样就不会出现, 数据不一致,而后致使崩溃的问题了.
以上是个人解决方案,要是你有更好的,能够告诉我哈 
相关文章
相关标签/搜索