iOS Development

ios – Crash associated to UICollectionViewDiffableDataSource


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.title
        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 stop reuse and trigger created cells to stay inaccessible in reminiscence for the lifetime of the gathering view. Registrations needs to be created up entrance and reused. Registration: <UICollectionViewCellRegistration: 0x600003353a80>"

What's your reaction?

Leave A Reply

Your email address will not be published.