【发布时间】:2016-05-09 23:06:40
【问题描述】:
我喜欢 Dwift,但我更希望将它与 ReactiveCocoa 一起使用,以帮助进一步降低我的集合视图控制器中的代码复杂性。
目前,我有一个辅助类,它采用 SignalProducer<[[T]]> 的实例,其中 T: Equatable (因此它适用于不同的情况)。每次信号生产者发出一个新值时:
self.data.producer.observeOn(UIScheduler()).on(next: { [unowned self] in
guard let collectionView = self.collectionView else { return }
for (index, element) in $0.enumerate() {
if index == self.diffCalculators.count {
let calculator = CollectionViewDiffCalculator<T>(collectionView: collectionView, initialRows: element)
calculator.sectionIndex = index
self.diffCalculators.append(calculator)
} else {
let calculator = self.diffCalculators[index]
calculator.rows = element
}
for index in self.data.value.count..<(self.diffCalculators.count >= self.data.value.count ? self.diffCalculators.count : self.data.value.count) {
self.diffCalculators.removeAtIndex(index)
}
}
})
.takeUntil(self.willDeallocSignal())
.start()
在这里,在枚举我的二维数组时,如果还没有差异计算器,则会创建一个并将其添加到我的存储数组 diffCalculators。如果确实存在,则设置rows 属性。之后,我循环遍历其余部分并删除它们。
不幸的是,我一直无法成功地让它发挥作用。我一次又一次收到The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (0), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).',但我不知道这是我的逻辑还是我使用 Dwift 错误。
有什么建议吗?
赏金编辑:
作为参考,这是我构建的帮助器类,用于将集合视图与反应性可可数据绑定在一起:https://gist.github.com/startupthekid/b3a69363d83e2279da0d750959c5a930
我需要的是一种以反应式、线程安全的方式生成和修改CollectionViewDiffCalculators 的方法。目前使用副作用崩溃取决于我接收新数据的速度(计算一个差异,数据进入,并且集合视图同时尝试重新加载)。
【问题讨论】:
-
这是由于填充集合视图的数据结构中的插入或删除不一致而发生的。你调试了吗?
-
是的,我做到了,问题不在我的数据结构中,我会发布答案并解释发生了什么。
标签: ios swift reactive-cocoa