Fatal: supplied item identifiers are not unique. Duplicate identifiers in DiffableDataSource

593 views Asked by At

I'm trying to use DiffableDataSource collection view, I keep getting this error

Fatal: supplied item identifiers are not unique. Duplicate identifiers

Here is how I implement it

extension UICollectionView {
    enum Section {
        case main
    }
}

class FeedsCollectionView: View {
    
    var collectionView: UICollectionView! = nil
    let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
    
    typealias DataSource = UICollectionViewDiffableDataSource<UICollectionView.Section, Scraper>
    typealias DataSourceSnapshot = NSDiffableDataSourceSnapshot<UICollectionView.Section, Scraper>
    
    private var dataSource : DataSource!
    private var snapshot: DataSourceSnapshot!
    
    public lazy var data: [Scraper] = [] {
        didSet {
            
            applySnapshot()

        }
    }
    
    private func configureDataSource() {
        dataSource = DataSource(collectionView: collectionView, cellProvider: { (collectionView, indexPath, data) in
            if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "feeds", for: indexPath) as? FeedsCollectionViewCell {
                cell.configure(with: data)
                
                cell.eventHandler = { [weak self] events in

                    switch events {

                    case .reload:
                       
                        
                        self?.applySnapshot()
                        
                    }

                }
                
                return cell

            }
            
            return UICollectionViewCell()
        })
        applySnapshot()
    }
    
    public func applySnapshot() {
        snapshot = DataSourceSnapshot()
        
        snapshot.appendSections([UICollectionView.Section.main])
        snapshot.appendItems(data)
        
        dataSource.apply(snapshot, animatingDifferences: true)
    }
                                
    private func createLayout() -> UICollectionViewLayout {
        
        return UICollectionViewCompositionalLayout { section, layoutEnvironment in
            var config = UICollectionLayoutListConfiguration(appearance: .sidebar)
            config.headerMode = section == 0 ? .none : .firstItemInSection
            return NSCollectionLayoutSection.list(using: config, layoutEnvironment: layoutEnvironment)
        }
    }
    
    
    private func configureHierarchy() {

        collectionView = UICollectionView(frame: bounds, collectionViewLayout: createLayout())
        
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.register(FeedsCollectionViewCell.self, forCellWithReuseIdentifier: "feeds")


        addSubview(collectionView)
        
    }

    override func setupUI() {
        configureHierarchy()

        configureDataSource()
    }
    
    override func setupLayout() {
        LayoutConstraint.pin(to: .bounds, collectionView).activate()
    }
    
}

and here the scrapper class(part of it) :

public class Scraper:  Hashable {
    
    let id = UUID()

    
    public func hash(into hasher: inout Hasher) {
      hasher.combine(id)
    }
    
    public static func == (lhs: Scraper, rhs: Scraper) -> Bool {
        lhs.id == rhs.id

    }

the Scraper class is much bigger and I can't make it struct.How I can fix this error Thank you so much

1

There are 1 answers

2
CloudBalancing On

It seems like you are adding the same items over and over...