TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Cell is not configured in collection view with Diffable data source, while using generics

Forums > Swift

@kaiju  

I have four models: Vegetables, Fruits, Herbs and Condiments. Each model has separate collection view, and different cell identifier. Data is successfully fetched which I confirmed by printing number of objects.

I have used DiffableDataSource, where data source is initialized as follows:

Data Source & snapshot:

var dataSource: UICollectionViewDiffableDataSource<String, NSManagedObjectID>?
typealias DataSource = UICollectionViewDiffableDataSource<String, NSManagedObjectID>
typealias Snapshot = NSDiffableDataSourceSnapshot<String, NSManagedObjectID>

Setting up data source:`

private func setupDataSource<T:NSManagedObject>(for collectionView:UICollectionView,
                                    with identifier: String,
                                    of entity: T.Type)
                                    ->DataSource
    {

         let dataSource = DataSource(
            collectionView: collectionView,
            cellProvider: {(collectionView, indexPath, managedObjectID) -> UICollectionViewCell? in

          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! KitchenCollectionViewCell

        if let pantry = try? self.kitchenCoreDataStack.managedContext.existingObject(with: managedObjectID) as? T {
          self.configure(cell: cell, for: pantry)
        }
        return cell
})
         return dataSource
    }

When breakpoint is set in above code, it doesn't go past this line:

cellProvider: {(collectionView, indexPath, manageObjectID)

Snapshot code to reflect changes: extension KitchenViewController: NSFetchedResultsControllerDelegate{

func applySnapshot(animatingDifferences: Bool){
    let snapshot = Snapshot()
    dataSource?.apply(snapshot, animatingDifferences: animatingDifferences)
}

Thank you.

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.