title: Diffable Data Source with a custom collection view layout?
body: <p><a href="https://github.com/AlexMarshall12/DiffableDataSourceTest" rel="nofollow noreferrer">Here</a> I have created a sample app that uses diffable data source for a collection view with a custom collection view layout. The specific layout I am using is from <a href="https://www.raywenderlich.com/4829472-uicollectionview-custom-layout-tutorial-pinterest" rel="nofollow noreferrer">this</a> tutorial.</p>
  <p>Here is the relevant part of the code if you don't want to clone the repo and try it for yourself.</p>
  <pre><code>import UIKit
  let cellIdentifier = &quot;testRecordCell&quot;
  
  struct Record:Hashable {
      let identifier = UUID()
      func hash(into hasher: inout Hasher) {
          hasher.combine(identifier)
      }
      static func == (lhs: Record, rhs: Record) -&gt; Bool {
          return lhs.identifier == rhs.identifier
      }
      var timeStamp: Date
      init(daysBack: Int){
          self.timeStamp = Calendar.current.date(byAdding: .day, value: -1*daysBack, to: Date())!
      }
  }
  class Cell:UICollectionViewCell {
  }
  
  class Section: Hashable {
    var id = UUID()
    // 2
    var records:[Record]
    
    init(records:[Record]) {
      self.records = records
    }
    
    func hash(into hasher: inout Hasher) {
      hasher.combine(id)
    }
    
    static func == (lhs: Section, rhs: Section) -&gt; Bool {
      lhs.id == rhs.id
    }
  }
  
  extension Section {
    static var allSections: [Section] = [
      Section(records: [
        Record(daysBack: 5),Record(daysBack: 6)
      ]),
      Section(records: [
        Record(daysBack: 3)
      ])
      ]
  }
  
  class ViewController: UICollectionViewController {
  
      private lazy var dataSource = makeDataSource()
      private var sections = Section.allSections
  
      fileprivate typealias DataSource = UICollectionViewDiffableDataSource&lt;Section,Record&gt;
      fileprivate typealias DataSourceSnapshot = NSDiffableDataSourceSnapshot&lt;Section,Record&gt;
      
      override func viewDidLoad() {
          super.viewDidLoad()
          self.collectionView?.register(Cell.self, forCellWithReuseIdentifier: cellIdentifier)
          applySnapshot()
          if let layout = collectionView.collectionViewLayout as? PinterestLayout {
              layout.delegate = self
          }
      }
  }
  extension ViewController {
      
      fileprivate func makeDataSource() -&gt; DataSource {
          let dataSource = DataSource(
              collectionView: self.collectionView,
              cellProvider: { (collectionView, indexPath, testRecord) -&gt;
                UICollectionViewCell? in
                  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath)
                  cell.backgroundColor = .black
                  return cell
              })
          return dataSource
      }
      func applySnapshot(animatingDifferences: Bool = true) {
          // 2
          var snapshot = DataSourceSnapshot()
          snapshot.appendSections(sections)
          sections.forEach { section in
            snapshot.appendItems(section.records, toSection: section)
          }
          //This part errors out: &quot;request for number of items in section 0 when there are only 0 sections in the collection view&quot;
          dataSource.apply(snapshot, animatingDifferences: animatingDifferences)
      }
  }
  extension ViewController: PinterestLayoutDelegate {
      func collectionView(_ collectionView: UICollectionView, heightForPhotoAtIndexPath indexPath: IndexPath) -&gt; CGFloat {
          return CGFloat(10)
      }
  }
  </code></pre>
  <p>Somehow, the layout is not registering that the collection view does have items and sections in it. When you run it normally, it errors out when you are trying to apply the snapshot: &quot;request for number of items in section 0 when there are only 0 sections in the collection view&quot;</p>
  <p>Then, in the prepare() function of the Pinterest layout, when I set a breakpoint and inspect collectionView.numberOfSections() it returns 0. So somehow the snapshot is not communicating with the collection view. Notice that I never use the collectionView's delegate method numberOfSections because I am using the diffable data source...</p>
  <p>My impression is that diffable data source is usually used with compositional layout though I have not seen anywhere that this is a requirement.</p>
  <p>So is there any way to do this?</p>