Crash related to UICollectionViewDiffableDataSource

Viewed 24

Following the Ray Wenderlich tutorial, I'm having trouble accessing the adoptions set.


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

var petCellRegistration: UICollectionView.CellRegistration<UICollectionViewListCell, Item> {
    return .init { cell, _, item in
        guard let pet = item.pet else { return }
        var configuration = cell.defaultContentConfiguration()
        configuration.text = pet.name
        configuration.secondaryText = "\(pet.age) years old"
        configuration.image = UIImage(named: pet.imageName)
        configuration.imageProperties.maximumSize = CGSize(width: 40, height: 40)
        cell.contentConfiguration = configuration

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

I'm seeing the following crash:

Thread 1: "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. Registration: <UICollectionViewCellRegistration: 0x600003353a80>"

0 Answers
Related