alextesy
alextesy

Reputation: 61

NSInternalInconsistencyException when reloading collectionView although without explanation

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?

Upvotes: 0

Views: 1279

Answers (0)

Related Questions