UICollectionViewDiffableDataSource crash

Why does returning nil from

Code Block
UICollectionViewDiffableDataSource<FeedElement, Entity>(collectionView: collectionView) {
            (collectionView: UICollectionView, indexPath: IndexPath, entity: Entity) -> UICollectionViewCell? in

result n "the collection view's data source did not return a valid cell from"

Isn't the UICollectionViewCell optional?
Answered by Frameworks Engineer in 620506022
The cell is optional just so that you can create proper switch of if...else cases in the block and don't have to create a face cell for something that is never hit. As with the old data source delegate methods, you have to return a valid cell when the collection view asks you to as this cell has a reserved spot in the layout and matches an existing entry in the data source, so it has to be represented somehow.
Accepted Answer
The cell is optional just so that you can create proper switch of if...else cases in the block and don't have to create a face cell for something that is never hit. As with the old data source delegate methods, you have to return a valid cell when the collection view asks you to as this cell has a reserved spot in the layout and matches an existing entry in the data source, so it has to be represented somehow.
Makes sense, thanks!

The cell is optional just so that you can create proper switch of if...else cases in the block and don't have to create a face cell for something that is never hit. As with the old data source delegate methods, you have to return a valid cell when the collection view asks you to as this cell has a reserved spot in the layout and matches an existing entry in the data source, so it has to be represented somehow. <

i found this strange... if some code path is never hit i can put "fatalError()" in there and then i will not have to return anything in that code path as fatalError is "noreturn"... and given that code is never hit it won't run either. it looks strange that cellProvider is typed to return optional.
filing the relevant suggestion to change cell provider return type to be non-optional. FB8052183

nomspes I eventually found all your questions regarding diffable data sources, including this one. Looks like you struggled a lot with this api. Since the forum does not allow direct messages between users :(, could you take a look at my small issue please and give your advice to me on this? Thank you

UICollectionViewDiffableDataSource crash
 
 
Q