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 Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.