Saturday, November 21, 2026
HomeiOS Developmentios - Crash associated to UICollectionViewDiffableDataSource

ios – Crash associated to UICollectionViewDiffableDataSource

[ad_1]

Following the Ray Wenderlich tutorial, I am having bother accessing the adoptions set.


// MARK: - Properties
var adoptions = Set<Pet>()
lazy var dataSource = makeDataSource()

var petCellRegistration: UICollectionView.CellRegistration<UICollectionViewListCell, Merchandise> {
    return .init { cell, _, merchandise in
        guard let pet = merchandise.pet else { return }
        var configuration = cell.defaultContentConfiguration()
        configuration.textual content = pet.identify
        configuration.secondaryText = "(pet.age) years outdated"
        configuration.picture = UIImage(named: pet.imageName)
        configuration.imageProperties.maximumSize = CGSize(width: 40, peak: 40)
        cell.contentConfiguration = configuration

        // THE LINE BELOW IS WHAT MAKES IT CRASH
        if self.adoptions.comprises(pet) {
          var backgroundConfig = UIBackgroundConfiguration.listPlainCell()
          backgroundConfig.backgroundColor = .systemBlue
          backgroundConfig.cornerRadius = 5
          backgroundConfig.backgroundInsets = NSDirectionalEdgeInsets(high: 5, main: 5, backside: 5, trailing: 5)
          cell.backgroundConfiguration = backgroundConfig
        }
    }
}

I am seeing the next crash:

Thread 1: "Tried to dequeue a cell utilizing a registration that was created inside -collectionView:cellForItemAtIndexPath: or inside a UICollectionViewDiffableDataSource cell supplier. Creating a brand new registration every time a cell is requested will forestall reuse and trigger created cells to stay inaccessible in reminiscence for the lifetime of the gathering view. Registrations must be created up entrance and reused. Registration: <UICollectionViewCellRegistration: 0x600003353a80>"

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments