swift - 如何处理响应式(Reactive)编程更新和CollectionView

标签 swift uicollectionview rx-swift reactive-swift

我有一个网络套接字,可以向 MutableProperty<[Stuff]> 添加或删除数据

但是当我重新加载collectionView时与 collectionView.reloadData() 。 Web 套接字向 MutableProperty<[Stuff]> 添加或删除数据,所以它会崩溃

由于多种原因,这里有一些错误:

  • 当 collectionView 方法使用我的列表时索引超出范围
  • 无效的项目数量会导致崩溃,例如:Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (58) must be equal to the number of sections contained in the collection view before the update (59), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

有人有好的建议或最佳实践吗? 我发现 MutableProperty 是线程安全的

我知道为什么会崩溃。它崩溃是因为 websockets 在 reloadData 期间添加或删除数组中的值。 collectionView的但我不知道如何解决。 我知道我们可以使用信号量,但我正在寻找一些优雅的东西

最佳答案

您应该向我们展示更多的代码,以便我们更好地理解。 但是,如果您在委托(delegate)中返回固定值:numberOfSectionsnumberOfRowsInSection,则需要在调用collectionView.reloadData()之前更新它们,以便这两个委托(delegate)返回节的确切数量以及每个节中的项目数。

关于swift - 如何处理响应式(Reactive)编程更新和CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043322/

相关文章:

ios - Alamofire 分段上传出现错误

json - 如何在 SwiftUI 中解码来自 API 的 JSON 响应以在 View 中使用

ios - 如何在使用自定义单元格的 UICollectionView 中正确设置异步图像下载?

swift - 更改 Collection View 中每 3 个单元格的宽度

ios - 在 iOS Swift 上从网络加载 MVVM 异步 UIImage

ios - Xcode 模拟器 - 导航栏丢失

ios - crash - 重击 @escaping @callee_guaranteed (@unowned CMTime)

ios - 为 CollectionView 的单独 Cell 设置圆形标签 [Swift]

swift - RxSwift和Moya同步请求

swift - RxSwift .map 表达式过于复杂,无法在合理的时间内解决;考虑将表达式分解为不同的子表达式