我有一个网络套接字,可以向 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)中返回固定值:numberOfSections
和numberOfRowsInSection
,则需要在调用collectionView.reloadData()
之前更新它们,以便这两个委托(delegate)返回节的确切数量以及每个节中的项目数。
关于swift - 如何处理响应式(Reactive)编程更新和CollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043322/