I'm trying to work with Diffable Data sources and have been hitting constant roadblocks. I have a very simple setup but am getting the "Could not cast value of type '_NSCoreDataTaggedObjectID' when using Diffable Data sources" when trying to apply my modified snapshot to the diffable data source.

I have a very simple setup:

My core data object class I'm using for this project is just this:

extension Employee {
    @nonobjc public class func fetchRequest() -> NSFetchRequest<Employee> {
        return NSFetchRequest<Employee>(entityName: "Employee")
    }

    @NSManaged public var employeeID: String?
    @NSManaged public var jobTitle: String?
    @NSManaged public var name: String?
    @NSManaged public var uuid: UUID?
}

extension Employee : Identifiable {}

My section struct is this:

enum JobTitle: String, CaseIterable {
    case programmer = "Programmer"
    case projectManager = "Project Manager"
    case officeWorker = "Office Worker"
}

And all I have in my NSFetchedResultsControllerDelegate extension is this method:

func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) {

        //Apple docs say to convert NSDiffableDataSourceSnapshotReference to NSDiffableDatasourceSnapshot
        var currentSnap = snapshot as NSDiffableDataSourceSnapshot<JobTitle, Employee>

        JobTitle.allCases.forEach { jobTitle in
            currentSnap.appendSections([jobTitle])
            let items = currentSnap.itemIdentifiers(inSection: jobTitle)
            currentSnap.appendItems(items, toSection: jobTitle)
        }

        dataSource.apply(currentSnap, animatingDifferences: true) //This crashes the app
    }

When I add a new Employee, it triggers the code above and crashes. I then get the following message:

Could not cast value of type '_NSCoreDataTaggedObjectID' (0x1ba229b60) to 'FetchTest.Employee' (0x10091e068).

I'm not sure why I'm getting this because screen grabs straight from WWDC videos shows Apple using custom enums as sections (since they're hashable) and objects as items. Is there something wrong in my code on how I'm approaching this? Or is there something I'm missing about the core data class that needs to be there for this to work? Googling and reviewing WWDC videos has turned up nothing.

0

There are 0 best solutions below