ios – Crash associated to UICollectionViewDiffableDataSource


Following the Ray Wenderlich tutorial, I am having hassle 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.title
        configuration.secondaryText = "(pet.age) years previous"
        configuration.picture = UIImage(named: pet.imageName)
        configuration.imageProperties.maximumSize = CGSize(width: 40, top: 40)
        cell.contentConfiguration = configuration

        // THE LINE BELOW IS WHAT MAKES IT CRASH
        if self.adoptions.incorporates(pet) {
          var backgroundConfig = UIBackgroundConfiguration.listPlainCell()
          backgroundConfig.backgroundColor = .systemBlue
          backgroundConfig.cornerRadius = 5
          backgroundConfig.backgroundInsets = NSDirectionalEdgeInsets(prime: 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>"

Leave a Reply