UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Xcode 13.0 beta shouts my lazily initialized UICollectionView.CellRegistration inside a separate datasource class

Forums > Swift

I have a separate datasource class that extends UICollectionViewDatasource and it contains cell registration. Sometimes, I want to get instance member inside my cell registration closure but the cell registration itself is in the initialization state so it can't get the instance member. To tackle that, I labeld my property as lazy so that it will get access to the instance member when it was used. I'm not sure this is a good way to do this but it works in XCode 12. When I build and run on XCode 13 beta, it crashed with NSInternalInconsistencyException.

Attempted to dequeue a cell using a registration that was created inside -collectionView:cellForItemAtIndexPath: or inside a UICollectionViewDiffableDataSource cell provider. Creating a new registration each time a cell is requested will prevent reuse and cause created cells to remain inaccessible in memory for the lifetime of the collection view. Registrations should be created up front and reused.

ScreenShot 1

ScreenShot 2

According to my understanding, lazy properties will be initialized as soon as they get called and only for once. So, I'm wondering if you could tell me if this is the problem with my XCode 13 or it's my problem. I'm also curious is it good to use lazy in that way? Or should I change them into static instead of instance properties? Thank you for reading till the end.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.