jflinter / Dwifft

Swift Diff

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Usage with ReactiveCocoa

thebarndog opened this issue · comments

I love Dwifft but I would love even more to use it with ReactiveCocoa to help reduce code complexity in my collection view controllers even more.

Currently, I have a helper class that takes an instance of SignalProducer<[[T]]> where T: Equatable (so it works with the differ). Every time the signal producer emits a new value:

        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()

Here, while enumerating through my 2d array, if a diff calculator doesn't exist yet, one is created and added to my storage array, diffCalculators. If one does exist, the rows property is set. Afterwards, I loop through the remaining sections and remove them.

Unfortunately, I've been incredibly unsuccessful in getting this to work. Time after time I get a 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).' and I can't tell if that's in my logic or if I'm using Dwifft wrong.

Any suggestions?

Hmm, maintaining several diff calculators on a single table view seems like a recipe for disaster IMO. It sounds like the issue isn't that you're using RAC, but that you don't know the number of sections you'll have. This will be solved by #18, but I can't say when I'll be able to get to it.