SwiftData - Assignment randomly failing

I have a SwiftData class which has an attribute of

var dimObject : DIM?

I'm trying to assign a DIM object to that attribute, buts its randomly failing with

Thread 1: EXC_BAD_ACCESS (code=1, address=0x8000000000000010)

Its never the same object, nor do I get any further indications of what's causing it.

I've narrowed it down to

digimon.dimObject = dimFound

in the below function

    func insertDigimon(digimon: Digimon) {
    Thread.sleep(forTimeInterval: 1)
    if let dimFound = getDIMFromID(dimID: digimon.dim) {
        print("****************************************************************")
        print("Digimon: \(digimon.name) - ID: \(digimon.id)")
        print("DIM: \(dimFound.name)")
        print("Set DIM Object on Digimon")
        digimon.dimObject = dimFound
        print("Digimon object set")
        print("Add digimon to DIM")
        dimFound.addToDigimon(d: digimon)
        print("Digimon added to DIM")
    }
    modelContext.insert(digimon)
}

If I comment out that line, there's no error

Both the digimon and dimFound objects aren't nil and contain the expected object.

SwiftData - Assignment randomly failing
 
 
Q