GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 try! Swift Tokyo.

SPONSORED Ready to dive into the world of Swift? try! Swift Tokyo is the premier iOS developer conference will be happened in April 9th-11th, where you can learn from industry experts, connect with fellow developers, and explore the latest in Swift and iOS development. Don’t miss out on this opportunity to level up your skills and be part of the Swift community!

Get your ticket here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.