stackunderflow
stackunderflow

Reputation: 852

How to implement multiple-entity UICollectionViewDiffableDataSource with Core Data?

I have a UICollectionView populated with a diffable data source being populated with Core Data managed objects from an NSFetchedResultsController. It is based on this tutorial by Antoine Van Der Lee. The data source is implemented as follows:

let diffableDataSource = UICollectionViewDiffableDataSource<Int, NSManagedObjectID>(collectionView: collectionView) { (collectionView, indexPath, objectID) -> UICollectionViewCell? in
    guard let object = try? managedObjectContext.existingObject(with: objectID) else {
        fatalError("Managed object should be available")
    }

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell_identifier", for: indexPath)
    /// ... Setup cell with the managed object
    return cell
}

/// Store the data source in an instance property to make sure it's retained.
self.diffableDataSource = diffableDataSource

/// Assign the data source to your collection view.
collectionView.dataSource = diffableDataSource

And the NSFetchedResultsControllerDelegate methods are implemented as follows:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {
    guard let dataSource = collectionView?.dataSource as? UICollectionViewDiffableDataSource<Int, NSManagedObjectID> else {
        assertionFailure("The data source has not implemented snapshot support while it should")
        return
    }
    var snapshot = snapshot as NSDiffableDataSourceSnapshot<Int, NSManagedObjectID>
    let currentSnapshot = dataSource.snapshot() as NSDiffableDataSourceSnapshot<Int, NSManagedObjectID>

    let reloadIdentifiers: [NSManagedObjectID] = snapshot.itemIdentifiers.compactMap { itemIdentifier in
        guard let currentIndex = currentSnapshot.indexOfItem(itemIdentifier), let index = snapshot.indexOfItem(itemIdentifier), index == currentIndex else {
            return nil
        }
        guard let existingObject = try? controller.managedObjectContext.existingObject(with: itemIdentifier), existingObject.isUpdated else { return nil }
        return itemIdentifier
    }
    snapshot.reloadItems(reloadIdentifiers)

    let shouldAnimate = collectionView?.numberOfSections != 0
    dataSource.apply(snapshot as NSDiffableDataSourceSnapshot<Int, NSManagedObjectID>, animatingDifferences: shouldAnimate)
}

This works perfectly for displaying a single entity in the collection view, in a single section. It can also show a single entity, in multiple sections, if the sectionNameKeyPath property is set on the fetched results controller.

However, I'm not sure how to make this work for a multiple entity setup where each entity would occupy its own section. I know that each entity would require its own fetched results controller, and that the cell provider closure can be modified to use the section index to determine what type of entity and cell to display.

What I don't know, is how to tell the diffable data source how many sections should be in the snapshot, because that is currently being dictated by the fetched results controller. I know the snapshot can be manually manipulated to add sections, however, with my current implementation the snapshot is being largely managed by the fetched results controller.

Does anyone have an example of a multiple-entity, Core Data populated collection view using a diffable data source and multiple fetched results controllers?

Upvotes: 0

Views: 44

Answers (0)

Related Questions