I'm trying to use NSFetchedResultsController with UICollectionView List. It works good, but when NSFetchedResultsController detect changes I get error: Could not cast value of type '_NSCoreDataTaggedObjectID' (0x7fff8091b280) to 'ProjectName.Person' (0x1053479e0).
It's my code:
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {
if dataSource==nil {
return
}
guard let dataSource = collectionView?.dataSource as? UICollectionViewDiffableDataSource<Int, Person> else {
return
}
var snapshot = snapshot as NSDiffableDataSourceSnapshot<Int, Person>
let currentSnapshot = dataSource.snapshot() as NSDiffableDataSourceSnapshot<Int, Person>
let reloadIdentifiers: [Person] = 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.objectID), existingObject.isUpdated else { return nil }
return itemIdentifier
}
snapshot.reloadItems(reloadIdentifiers)
let shouldAnimate = collectionView?.numberOfSections != 0
dataSource.apply(snapshot as NSDiffableDataSourceSnapshot<Int, Person>, animatingDifferences: shouldAnimate)
}
Please help