Shahbaz Ali
Shahbaz Ali

Reputation: 711

iOS 17 crash collectionView at batchUpdate

I am performing batchUpdate.

Here is my code:

if let footerIndex = self.collectionView.feeds.index(forKey: "footer") {
    self.collectionView.performBatchUpdates {
        self.collectionView.totalSections += 1
        self.collectionView.insertSections([footerIndex], animationStyle: .none)
        self.collectionView.insertItemsAtIndexPaths([IndexPath.init(row: 0, section: footerIndex)], animationStyle: .none)
    }
} 

In all earlier iOS versions it works, but iOS 17 it crashes:

*** 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 after performing the batch updates are inconsistent with the updates.
Data source before updates = { 47 sections with item counts: [1, 3, 1, 3, 1, 3, 1, 3, 1, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 1, 3, 1, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 1, 3, 1, 3, 1] }
Data source after updates = { 47 sections with item counts: [1, 11, 1, 3, 1, 3, 1, 3, 1, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 1, 3, 1, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 1, 3, 1, 3, 1] }
Updates = [
Insert item (1 - 0),
Insert item (1 - 1),
Insert item (1 - 2),
Insert item (1 - 3),
Insert item (1 - 4),
Insert item (1 - 5),
Insert item (1 - 6),
Insert item (1 - 7),
Insert item (1 - 8),
Insert item (1 - 9),
Insert item (1 - 10)
]

This is the error. Is there any work around for iOS 17?

Upvotes: 0

Views: 2204

Answers (1)

Umar Muzaffer
Umar Muzaffer

Reputation: 36

Normally, the practice should be in three ways:

  1. If you want to update overall collection view then you can use collectionView.reloadData ()

  2. If you want to reload sections of collection view you can use collectionView.reloadSections ()

  3. If you want to reload the specific cell of collection view you can use collectionView.reloadItems(at: section)

Please always use array to append/delete items and then update collectionView.

Upvotes: 0

Related Questions