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:
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)
}
}
I called datasource function with :
dataSource = setupDataSource(for: firstCollectionView, with: "fruits", of: Fruits.self)