UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free 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.