Crash in UICollectionViewCompositionalLayout iOS 13 with decoration views

I have two section backgrounds for my compositional layout.

Code Block swift
layout.register(
    FollowingSectionBackgroundView.self,
    forDecorationViewOfKind: FollowingCollectionViewController.sectionBackgroundDecorationFollowing)
layout.register(
    BrowseSectionBackgroundView.self,
    forDecorationViewOfKind: FollowingCollectionViewController.sectionBackgroundDecorationBrowse)

And I configure them like so in my UICollectionViewCompositonaLayoutClosure

Code Block swift
        if sectionIndex == 0 {
            let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(
                elementKind: FollowingCollectionViewController.sectionBackgroundDecorationFollowing)
            section.decorationItems = [sectionBackgroundDecoration]
        } else {
            let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(
                elementKind: FollowingCollectionViewController.sectionBackgroundDecorationBrowse)
            section.decorationItems = [sectionBackgroundDecoration]
        }


And this seems to be working just fine, however I have seen crashes uploaded that say:
Code Block
Fatal Exception: NSInternalInconsistencyException
no UICollectionViewLayoutAttributes instance for -layoutAttributesForDecorationViewOfKind: section-background-browse at path <NSIndexPath: 0xfe6f76a7b11ddfee> {length = 2, path = 0 - 0}


The crash appears to happen when the user searches.
Code Block swift
        isSearching = true
        let following = FollowingController.filterBrowse(with: filter).sorted { $0.name < $1.name }
        var snapshot = NSDiffableDataSourceSnapshot<SectionLayoutKind, FollowingEntity>()
        snapshot.appendSections([.browse])
        snapshot.appendItems(following)
        dataSource.apply(snapshot, animatingDifferences: true)


Im unable to reproduce this issue but just curious if anyone has run into this, and might have an idea for solving it.
Answered by nomspes in 618998022
Okay, I resolved this by invalidating the collectionViewLayout when preforming a search. and checking if we were searching when applying the decoration items.

Code Block swift
    func configureBackgroundViews(for section: NSCollectionLayoutSection, at sectionIndex: Int) {
        if sectionIndex == 0 && isSearching == false {
            let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(
                elementKind: FollowingCollectionViewController.sectionBackgroundDecorationFollowing)
            section.decorationItems = [sectionBackgroundDecoration]
        } else {
            let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(
                elementKind: FollowingCollectionViewController.sectionBackgroundDecorationBrowse)
            section.decorationItems = [sectionBackgroundDecoration]
        }
    }

Accepted Answer
Okay, I resolved this by invalidating the collectionViewLayout when preforming a search. and checking if we were searching when applying the decoration items.

Code Block swift
    func configureBackgroundViews(for section: NSCollectionLayoutSection, at sectionIndex: Int) {
        if sectionIndex == 0 && isSearching == false {
            let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(
                elementKind: FollowingCollectionViewController.sectionBackgroundDecorationFollowing)
            section.decorationItems = [sectionBackgroundDecoration]
        } else {
            let sectionBackgroundDecoration = NSCollectionLayoutDecorationItem.background(
                elementKind: FollowingCollectionViewController.sectionBackgroundDecorationBrowse)
            section.decorationItems = [sectionBackgroundDecoration]
        }
    }

Crash in UICollectionViewCompositionalLayout iOS 13 with decoration views
 
 
Q