RxSwift Differ库崩溃`NSInternalInconsistencyException`

问题描述 投票:1回答:1

我现在在一个基本示例中尝试将DiffingRxSwift结合使用。我正在使用Differ库。

这里是我的Interactor + ViewModel的关联:

import Foundation
import RxSwift
import RxCocoa

class Interactor {

    var items = [
            [1,5,6,7,4,6,7,1,5],
            [1,5,2,1,0,6,7],
    ]

    let viewModel: BehaviorRelay<ViewModel>

    var currentObjects: Int = 0 {
        didSet {
            viewModel.accept(.init(with: .loaded(items[currentObjects])))
        }
    }

    init() {
        viewModel = BehaviorRelay(value: .init(with: .initialized))
    }

    func fetchValue() {
        currentObjects = currentObjects == 0 ? 1 : 0
    }


}

struct ViewModel {

    enum ViewModelType: Equatable {
        case cell(CellViewModel)
    }

    enum State {
        case initialized
        case loaded([Int])
    }

    let state: State
    let viewModels: [ViewModelType]

    init(with state: State) {
        self.state = state
        switch state {
        case .initialized: viewModels = []
        case .loaded(let values):
            viewModels = CellViewModel.from(values).map(ViewModelType.cell)
        }
    }
}

extension ViewModel: Equatable {

    static func ==(left: ViewModel, right: ViewModel) -> Bool {
        return left.state == left.state
    }
}

extension ViewModel.State: Equatable {

    static func ==(left: ViewModel.State, right: ViewModel.State) -> Bool {
        switch (left, right) {
        case (.initialized, .initialized): return true
        case let (.loaded(l), .loaded(r)): return l == r
        default: return false
        }
    }
}

struct CellViewModel {
    let description: String
}

extension CellViewModel {

    static func from(_ values: [Int]) -> [CellViewModel] {
        return values.map { CellViewModel(description: String($0)) }
    }
}

extension CellViewModel: Equatable {

    static func ==(left: CellViewModel, right: CellViewModel) -> Bool {
        return left.description == right.description
    }
}

现在是视图,我正在使用一个简单的`UITableView

import UIKit
import Differ
import RxSwift

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        ...

        interactor
            .viewModel
            .asObservable()
            .scan([], accumulator: { (previous, current) in
              Array(previous + [current]).suffix(2)
            })
            .map({ (arr) -> (previous: ViewModel?, current: ViewModel) in
              (arr.count > 1 ? arr.first : nil, arr.last!)
            }).subscribe(onNext: { [weak self] (previous, current) in
                if let prev = previous {
                    print("Previous => State: \(prev.state) | ViewModelType.count: \(prev.viewModels.count)")
                } else {
                    print("Previous => State: nil | ViewModelType.count: nil")
                }
                print("Current => State: \(current.state) | ViewModelType.count: \(current.viewModels.count)")
                guard let strongSelf = self else { return }
                DispatchQueue.main.async {
                    strongSelf.tableView.animateRowChanges(oldData: previous?.viewModels ?? [], newData: current.viewModels)
                }
            }).disposed(by: disposeBag)

        interactor.fetchValue()
    }

    @objc
    func onRefresh() {
        interactor.fetchValue()
    }
}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return interactor.viewModel.value.viewModels.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellViewModel = interactor.viewModel.value.viewModels[indexPath.row]
        switch cellViewModel {
        case .cell(let viewModel):
            let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
            cell.textLabel?.text = viewModel.description
            return cell
        }
    }
}

在一切都符合Equatable的情况下,我以为可以完成工作,但是我遇到了NSInternalInconsistencyException例外。

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (7 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

我在崩溃显示前检查Rx的打印内容:

Previous => State: nil | ViewModelType.count: nil
Current => State: initialized | ViewModelType.count: 0
Previous => State: initialized | ViewModelType.count: 0
Current => State: loaded([1, 5, 2, 1, 0, 6, 7]) | ViewModelType.count: 7

从逻辑角度看,该流程对我来说看起来不错。我想念什么吗?


编辑2019/10/29

我制作了另一个版本而没有使用RxSwift来确定问题是否归咎于RxSwift

protocol InteractorDelegate: class {
    func viewModelDidChange(_ old: ViewModel?, _ new: ViewModel)
}

class Interactor {

    weak var delegate: InteractorDelegate?

    var items = [
            [1,5,6,7,4,6,7,1,5],
            [1,5,2,1,0,6,7],
    ]

    var viewModel: ViewModel? {
        didSet {
            delegate?.viewModelDidChange(oldValue, viewModel!)
        }
    }

    var currentObjects: Int = 0 {
        didSet {
            viewModel = .init(with: .loaded(items[currentObjects]))
        }
    }

    init() {
        viewModel = .init(with: .initialized)
    }

    func fetchValue() {
        currentObjects = currentObjects == 0 ? 1 : 0
    }
}

对于ViewController

extension ViewController: InteractorDelegate {

    func viewModelDidChange(_ old: ViewModel?, _ new: ViewModel) {

        if let prev = old {
            print("Previous => State: \(prev.state) | ViewModelType.count: \(prev.viewModels.count)")
        } else {
            print("Previous => State: nil | ViewModelType.count: nil")
        }
        print("Current => State: \(new.state) | ViewModelType.count: \(new.viewModels.count)")
        DispatchQueue.main.async {
            self.tableView.animateRowChanges(oldData: old?.viewModels ?? [], newData: new.viewModels)
        }
    }
}

似乎没有RxSwift,问题仍然存在:

Previous => State: initialized | ViewModelType.count: 0
Current => State: loaded([1, 5, 2, 1, 0, 6, 7]) | ViewModelType.count: 7
2019-10-29 13:45:56.636678+0900 TestDiffer[93631:21379549] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) must be equal to the number of rows contained in that section before the update (7), plus or minus the number of rows inserted or deleted from that section (7 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

我的Equatable的符合性有问题吗?

ios swift uitableview rx-swift
1个回答
0
投票

如果实现RxTableViewDataSourceType并将其传递给表视图的items运算符会更好。执行此操作的示例项目位于:https://github.com/danielt1263/RxMultiCounter

我在示例代码中使用了DifferenceKit,因此您将不得不在tableView(_:observedEvent:)方法中进行一些更改,但这应该有助于向正确的方向指出。

class RxSimpleAnimatableDataSource<E, Cell>: NSObject, 
    RxTableViewDataSourceType, 
    UITableViewDataSource 
    where E: Differentiable, Cell: UITableViewCell 
{
    typealias Element = [E]

    init(identifier: String, with animation: UITableView.RowAnimation = .automatic, configure: @escaping (Int, E, Cell) -> Void) {
        self.identifier = identifier
        self.animation = animation
        self.configure = configure
    }

    func tableView(_ tableView: UITableView, observedEvent: Event<Element>) {
        let source = values
        let target = observedEvent.element ?? []
        let changeset = StagedChangeset(source: source, target: target)
        tableView.reload(using: changeset, with: animation) { data in
            self.values = data
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return values.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as! Cell
        let row = indexPath.row
        configure(row, values[row], cell)
        return cell
    }

    let identifier: String
    let animation: UITableView.RowAnimation
    let configure: (Int, E, Cell) -> Void
    var values: Element = []
}
© www.soinside.com 2019 - 2024. All rights reserved.