ios – 捕获UICollectionView在Swift中执行批量更新断言失败?

ios – 捕获UICollectionView在Swift中执行批量更新断言失败?,第1张

概述我正在使用UICollectionView的 performBatchUpdates(_:completion:)方法.问题是,有时我的复杂差异逻辑会失败并返回不正确数量的要插入的部分.这会导致我插入的项目数与从数据源报告的数量不匹配.每当发生这种情况时,我们会收到以下错误: Assertion failure in -[CollectionView _endItemAnimationsWithI 我正在使用UICollectionVIEw的 performBatchUpdates(_:completion:)方法.问题是,有时我的复杂差异逻辑会失败并返回不正确数量的要插入的部分.这会导致我插入的项目数与从数据源报告的数量不匹配.每当发生这种情况时,我们会收到以下错误:

Assertion failure in -[CollectionVIEw _endItemAnimationsWithInvalIDationContext:tentativelyForReordering:animator:]

Error when performing batch updates: InvalID update: invalID number of sections. The number of sections contained in the collection vIEw after the update (25) must be equal to the number of sections contained in the collection vIEw before the update (19),plus or minus the number of sections inserted or deleted (0 inserted,0 deleted).

我意识到正确的解决方案是修复我的差异逻辑,使得返回的项目数和我调用的插入数之间没有不匹配.

但是,我想做的是使它即使我的逻辑在将来失败,而不是碰撞应用程序,它只会重新加载集合视图的数据.

我怎么能在Swift中做到这一点?

解决方法 默认情况下,无法在Swift中捕获NSExceptions,因此请创建类似于 this one或 this one的桥.

即使你发现断言失败,the user cannot interact with the collection view,所以你需要重新创建集合视图.

TryCatch.try({  collectionVIEw.performBatchUpdates({    collectionVIEw.insertItems(at: indexPaths)    collectionVIEw.insertSections(sections)  },completion: nil)},catch: { exception in  print("Error updating collection vIEw: \(exception)")  collectionVIEw.removeFromSupervIEw()  // recreate the collection vIEw (make sure to set datasource and delegates)  collectionVIEw = ...  collectionVIEw.dataSource = ...  collectionVIEw.delegate = ...},finally: nil)
总结

以上是内存溢出为你收集整理的ios – 捕获UICollectionView在Swift中执行批量更新断言失败?全部内容,希望文章能够帮你解决ios – 捕获UICollectionView在Swift中执行批量更新断言失败?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1067712.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存