更新无效:反应性编程问题第 0 节中的项目数无效

Invalid update: invalid number of items in section 0 in Reactive Programming issue

我遇到了很大的麻烦,因为当我在 collectionView 中选择一个单元格并更改我的数组数据时,我收到了这条错误消息:

'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (2) must be equal to the number of items contained in that section before the update (2), plus or minus the number of items inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

它发生在 iOS 13 之前。在 iOS 13 和 Upper 中运行良好。有我的代码:

import UIKit
import ReactiveKit
import Bond

//Its in my viewModel

var arrayOfObjects = MutableObservableArray<MyObject>([])


  actionCollectionViewDidSelect.observeNext { [weak self] indexPath in
        guard indexPath.row != 0 else { return }
        guard let object = self?.arrayOfPhotoObjects[indexPath.row] else { return }
        photoObject.isSelected = !photoObject.isSelected
        if let value = self?.arrayOfObjects.value { self?.arrayOfPhotoObjects.value = value } // Gives error because there but I can't find solution.
    }.dispose(in: disposeBag)

//Its in my ViewController


collectionView.reactive.selectedItemIndexPath.observe(with: viewModel.actionCollectionViewDidSelect).dispose(in: disposeBag)

    viewModel.arrayOfObjects.bind(to: collectionView) { object, indexPath, collectionView in
        
        let objectForIndex = arrayObjects[indexPath.row]
       
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "photoBoxCollectionViewCell", for: indexPath) as! MYCEll
             cell.data = objectForIndex
            return cell
        }
    }

我发现问题是在我尝试更改我的数组时遇到了麻烦。我的解决方案是:

  actionCollectionViewDidSelect.observeNext { [weak self] indexPath in
    guard indexPath.row != 0 else { return }
    guard let object = self?.arrayOfPhotoObjects[indexPath.row] else { return }
    photoObject.isSelected = !photoObject.isSelected
    if let value = self?.arrayOfObjects.value {                     
            let collection = value.collection
            let newArrayObject = MutableObservableArray<PhotoObject(collection)
            self?.arrayOfPhotoObjects.value = newArrayObject.value }
   }.dispose(in: disposeBag)