I'm also running into this issue, but a bit of type finagling I have it mostly working. My remaining issue is that I can't figure out how to properly populate the collection view on load.
My type order is, as IngmarStein suggests, string before object ID,
typealias Snapshot = NSDiffableDataSourceSnapshotReference<NSString, NSManagedObjectID>
typealias DataSource = UICollectionViewDiffableDataSourceReference<NSString, NSManagedObjectID>
extension ViewController: NSFetchedResultsControllerDelegate {
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference<NSManagedObjectID, NSString>) {
let newSnapshot = Snapshot()
newSnapshot.appendSections(withIdentifiers: snapshot.sectionIdentifiers as [AnyObject] as! [NSString])
newSnapshot.appendItems(withIdentifiers: snapshot.itemIdentifiers as [AnyObject] as! [NSManagedObjectID])
self.dataSource.applySnapshot(newSnapshot, animatingDifferences: true)
}
}
The secionIdentifiers are constant each launch, but I can't see how to get them out of NSFetchedResultsController without calling didChangeContent. Notably, section.name is nil. I can populated on view load by inventing temporary section titles
let snapshot = Snapshot()
if let sections = self.fetched.sections {
let sectionMap = [NSString: NSFetchedResultsSectionInfo](uniqueKeysWithValues: sections.map {section -> (NSString, NSFetchedResultsSectionInfo) in return (UUID().description as NSString, section)})
snapshot.appendSections(withIdentifiers: sectionMap.compactMap {$0.key})
for section in sectionMap {
snapshot.appendItems(withIdentifiers: (section.value.objects as! [NSManagedObject]).map {$0.objectID}, intoSectionWithIdentifier: section.key)
}
}
self.dataSource.applySnapshot(snapshot, animatingDifferences: false)
These IDs will be replaced on first change. I don't know what effect this might have on a more complicated fetch, but so far it seems to not animate a large rearrangement of existing objects into a new section.
I have also submitted a feedback request, FB6162037.