Applying NSDiffableDataSourceSnapshot to UICollectionViewDiffableDataSource causes 'NSInternalInconsistencyException'
Asked Answered
E

1

5

I am trying to implement a UICollectionViewDiffableDataSource for my collectionView. My code compiles fine, however I keep running into this error the first time I apply a snapshot to it, with the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: self.supplementaryViewProvider || (self.supplementaryReuseIdentifierProvider && self.supplementaryViewConfigurationHandler)'

Here is my code:

    var groups: [Group] = [Group]()
    var dataSource: UICollectionViewDiffableDataSource<Section, Group>!

    // MARK: - View Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        self.searchBar.delegate = self
        self.groups = DummyData.groups

        setupDataSource()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        performSearch(searchQuery: nil)
    }


    // MARK: - Helper Functions
    func performSearch(searchQuery: String?) {
        let filteredGroups: [Group]
        if let searchQuery = searchQuery, !searchQuery.isEmpty {
            filteredGroups = groups.filter { $0.contains(query: searchQuery) }
        } else {
            filteredGroups = groups
        }

        var snapshot = NSDiffableDataSourceSnapshot<Section, Group>()
        snapshot.appendSections([.main])
        snapshot.appendItems(filteredGroups, toSection: .main)
        dataSource.apply(snapshot, animatingDifferences: true, completion: nil)
    }

    func setupDataSource() {
        dataSource = UICollectionViewDiffableDataSource <Section, Group>(collectionView: collectionView) { (collectionView: UICollectionView, indexPath: IndexPath, group: Group) -> UICollectionViewCell? in

            guard let cell = self.collectionView.dequeueReusableCell(
                withReuseIdentifier: String(describing: MyGroupsCollectionViewCell.self), for: indexPath) as? MyGroupsCollectionViewCell else {
                    fatalError("Cannot create new cell") }

            cell.configure(withGroup: group)

            return cell
        }
    }

If needed, I can post the full call stack.

Elishaelision answered 18/11, 2019 at 15:27 Comment(0)
E
11

Found the answer. I was using the storyboard to create my collectionView and accidentally had the attribute for Section Header set to true. Because of this, the collectionView needed to pull the view for the section header for somewhere, but I never told it where, hence the

parameter not satisfying: self.supplementaryViewProvider || (self.supplementaryReuseIdentifierProvider && self.supplementaryViewConfigurationHandler)

Here's a good article I found on it for anyone in the future who runs into this issue: https://medium.com/@jamesrochabrun/uicollectionviewdiffabledatasource-and-decodable-step-by-step-6b727dd2485

Elishaelision answered 18/11, 2019 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.