Lazy cell registration causes crash while dequeue the collectionview cell in collectionview

Cell Registration

lazy var headerCell: UICollectionView.SupplementaryRegistration<UICollectionReusableView> = .init(elementKind: UICollectionView.elementKindSectionHeader) { supplementaryView, elementKind, indexPath in }

and

Cell Dequeue

datasource.supplementaryViewProvider = { [weak self] collectionView, kind, indexPath in return collectionView.dequeueConfiguredReusableSupplementary(using: headerCell, for: indexPath) }

I am registering a collectionview cell or collwctionview reusable view as lazy variable. It causes a crash like

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Attempted to dequeue a supplementary view using a registration that was created inside -collectionView:viewForSupplementaryElementOfKind:atIndexPath: or inside a UICollectionViewDiffableDataSource supplementary view provider. Creating a new registration each time a supplementary view is requested will prevent reuse and cause created supplementary views to remain inaccessible in memory for the lifetime of the collection view. Registrations should be created up front and reused. Registration: <UICollectionViewSupplementaryRegistration: 0x600001798a00>

Lazy variables are evaluated on first use, so if your variable is first evaluated inside of the places where the exception is firing, then thats going to be what UIKit sees. We don't know the deeper context here (that your actually preserving the value) just that your first registering it inside places which normally would not lead to reuse.

I would recommend you try to trigger your lazy variables to evaluate earlier than they are (in a known safe place) or not use lazy variables for this in the first place.

Written by Frameworks Engineer in 827001022
I would recommend you try to trigger your lazy variables to evaluate earlier

Hello Team,

I think this is not a optimal solution.

As we know, lazy variables are designed to be initialized only once upon their first access, ensuring efficient memory usage. However, I’ve encountered a situation where UIKit seems to assume that supplementary view registrations are being recreated each time a supplementary view is requested. This is leading to the following warning:

"Creating a new registration each time a supplementary view is requested will prevent reuse and cause created supplementary views to remain inaccessible in memory for the lifetime of the collection view. Registrations should be created up front and reused."

This behavior appears to conflict with the intended use of lazy variables, which should only be initialized once. It seems that UIKit is not recognizing the lazy initialization pattern. If a cell is registered lazily, UIKit should reuse it, right?

Is this an expected behavior, or could it be an issue with how UIKit handles lazy variables in this context? Any clarification or guidance on this matter would be greatly appreciated.

Thank you for your attention to this issue.

Lazy cell registration causes crash while dequeue the collectionview cell in collectionview
 
 
Q