Diffable Data Source: Invalid Section

I'm implementing a search bar in a collection view with diffable data source, and whenever I search a non-existent item twice (if I type a search that returns no matches, delete one character so it returns matches, then type that character again), the app crashes with NSInternalInconsistencyException: Invalid Section 2.

The crash occurs when I apply a new snapshot to the datasource.

Can anyone tell me what this exception means? There doesn't seem to be any documentation of it anywhere, but it looks like an issue around creating diffable data source snapshots.

See related (not mine) Stackoverflow post here: link

This indicates that compositional layout is trying to resolve layout for a section that doesn't exist in the data source. Are you using any index path related APIs and are by any chance referring to a section that doesn't exist at that time?

If you can isolate the issue into a sample project, filing this via the feedback assistant would certainly be helpful.

This indicates that compositional layout is trying to resolve layout for a section that doesn't exist in the data source.

This was the issue on my case, however my data source was returning the correct sections (like if the compositional layout had a cache), invalidating the layout worked for me:

collectionView.collectionViewLayout.invalidateLayout()

Diffable Data Source: Invalid Section
 
 
Q