Xcode15 crash reloadItems - Invalid update: invalid number of items in section

Xcode15 crash reloadItems - Invalid update: invalid number of items in section

When building with xcode14, there was no crash when calling collectionView reloadItems.

If you build with xcode15 A crash pops up saying Invalid update: invalid number of items in section.

The current issue has been resolved, but I would like to know the exact cause of the crash in xcode 15. Does anyone know?

Not really possible to figure this out without any code.

hello

When I just called collectionView.reloadItems, the total number changed.

When testing, if you run it with Xcode 14.1, it does not crash and reloadData.

It crashed when built with Xcode 15.2.

I knew it was originally wrong, but I would like to know the reason for this change and the changes in the processing method for reloadItems.

Accepted Answer

Previously, for invalid batch updates UIKit was silently falling back to reload data previously in some cases, resulting in broken animations in certain cases. This is now a hard exception. The exception message should have more information on how to solve this problem; essentially your data source counts before and after the update must line up with the updates you submit.

Remember, reloads are decomposed into a delete an an insert. A simple example here:

Consider a data source with the following section counts: [10, 20, 30]

If you submit an update with

INSERT(0,0) // section 0, item 0
RELOAD(1,1)  // section 1, item 1
INSERT(1, 2) // section 1, item 2
DELETE(2,1) // section 2, item 1

Then your data source counts must move to the updated values inside the performBatchUpdates: block. In the case of the above updates, that means, for the above update, your data source's implementation of numberOfItemsInSection needs to return

11 for section 0, since one item was inserted

21 for section 1, since one item was inserted and one was reloaded

29 for section 2, since one item was deleted

This can all be tricky to manage, which is why I highly recommend using diffable data source, which will save you the trouble of having to perform this diffing yourself

Xcode15 crash reloadItems - Invalid update: invalid number of items in section
 
 
Q