I'm currently working on a simple IOS app using a collection view and diffable datasource.
I initalize the everything in the view controller's config and call a reloadView-function every time I update the source data.
The app crashes with the following error as soon as I call the reloadView function. But only if the collection view was empty before. If there are already items in there everything works perfectly fine.
Assertion failure in -[_UICollectionCompositionalLayoutSolver _queryClientForSectionDefintionForSectionIndex:]
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid section definition. Please specify a valid section definition when content is to be rendered for a section. This is a client error.'
This is what my code looks like:
private func configureHierarchy() {
        let layout = collectionViewHelper.createLayout(structure: self.structure)
        collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.register(RecentCollectionViewCell.self, forCellWithReuseIdentifier: RecentCollectionViewCell.reuseIdentifier)
        collectionView.register(OverviewCollectionViewCell.self, forCellWithReuseIdentifier: OverviewCollectionViewCell.reuseIdentifier)
        collectionView.register(MessageItemCollectionViewCell.self, forCellWithReuseIdentifier: MessageItemCollectionViewCell.reuseIdentifier)
        view.addSubview(collectionView)
    }
private func configureDataSource() {
        dataSource = UICollectionViewDiffableDataSource<SectionType, Int>(collectionView: collectionView) {
            (collectionView: UICollectionView, indexPath: IndexPath, cellIndex: Int) -> UICollectionViewCell? in
            let sectionItem = self.structure[indexPath.section]
            switch sectionItem.type{
                //Returns a collection view cell of the specific type
            case .recent:
                return getRecentItemCell(...)
            case .overview:
                return getOverviewItemCell(...)
            case .message:
                return getMessageItemCell(...)
            default:
                return nil
            }
        }
    }
I apply the snapshot like this
private func applySnapshot(structure:[SectionItem]) {
        var snapshot = NSDiffableDataSourceSnapshot<SectionType, Int>()
        var itemOffset = 0
        for structurItem in structure {
            snapshot.appendSections([structurItem.type])
            snapshot.appendItems(Array(itemOffset..<itemOffset + structurItem.items))
            itemOffset += structurItem.items
        }
        dataSource.apply(snapshot, animatingDifferences: false)
    }
I do this once in my configure
structure = createStructure()
configureHierarchy()
configureDataSource()
applySnapshot(structure: createStructure())
This is the reload function I call every time the data changes (The error is thrown if there was no data displayed before)
func reloadView() {
        structure = createStructure()
        applySnapshot(structure: structure)
    }
Any ideas why this is? Thanks a lot already!
In my case, the UICollectionViewCompositionalLayout is not deleted in memory, it continues to work when my controller is already deleted.
private func configureLayout() {
    let layout = UICollectionViewCompositionalLayout { [weak self] (sectionIndex: Int, layoutEnvironment: NSCollectionLayoutEnvironment) -> NSCollectionLayoutSection? in
        return self?.layoutSection(sectionIndex: sectionIndex, layoutEnvironment: layoutEnvironment)
    }
    self.collectionView.collectionViewLayout = layout
}
its my code
In my case, my data source is set up wrongly. I'm returning more sections than I actually provide in UICollectionViewCompositionalLayout(sectionProvider: sectionProvider).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With