0

I am encountering an NSInternalInconsistencyException when trying to reload my UICollectionView. The error message states:

Invalid batch updates detected: the number of sections and/or items returned by the data source before and/or after performing the batch updates are inconsistent with the updates.

The exception only occurs when I build my app using Xcode. However, when I install the same exact code from TestFlight, everything works as expected.

I believe that the issue is related to passing a different data to the collection view than what was there before, even though it's supposed to be the same. The exception happens in the updateCollection() function shown below.

Code snippet:

@objc private func updateCollection() {
    let oldResults = viewModel.results
    self.viewModel.updateData { [weak self]  in
        if let newResults = self?.viewModel.results {
            let changes = diff(old: oldResults, new: newResults)
            self?.collectionView.reload(changes: changes, section: 0, updateData: {
                self?.viewModel.results = newResults
            }, completion: { isFinished in
                if isFinished {
                    self?.collectionView.reloadData()
                    self?.updateEmptyHomeView()
                }
            })
        }
    }
}
func updateData(completion: @escaping () -> ()) {
    results.removeAll()
    let uid = self.id
    if let user = DataManager.getUser(id: uid) {
        self.results = user.getData()
        completion()
    } else {
        completion()
    }
}

This is the full error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid batch updates detected: the number of sections and/or items returned by the data source before and/or after performing the batch updates are inconsistent with the updates. Data source before updates = { 1 section with item counts: [8] } Data source after updates = { 1 section with item counts: [8] }

Can anyone help me identify what might be causing this inconsistency and why it only occurs when I build the app in Xcode?

HangarRash
  • 7,314
  • 5
  • 5
  • 32
alextesy
  • 61
  • 6
  • I have solved the issue by downgrading the Xcode to 14.1 version and updating the support of IOS16.4. Some why this crashes in Xcode 14.3 – alextesy May 17 '23 at 05:36

0 Answers0