ios – SwiftData EXC_BREAKPOINT when setting relatinoship


Working with SwiftData for the first time and having trouble with crashes when I try to set the value of a relationship.

I have a Profession object which represents a job in the game. Each Profession can have expenses (Expense, like a mortgage payment).

I’m trying to populate the initial data when the app first launches. When I’m creating the first Profession, I create one Expense which seems to work but when I create a second Expense the app crashes with Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1c3c6203c) when trying to set the Expense.profession relationship. Xcode expands the profession property to show the get/set and it’s crashing in the set function.

Am I missing something obvious? Or is SwiftData expecting me to do something very different than CoreData would in this case? Any help is appreciated.

@Model
class Profession {
    var id: UUID
    var name: String
    
    @Relationship(deleteRule: .cascade, inverse: \Expense.profession) var expenses: [Expense] = []
        
    init(name: String) {
        self.id = UUID()
        self.name = name
    }
    
    convenience init(professiondefault: ProfessionDefault) {
        self.init(name: professiondefault.name)
        
        let taxes = Expense(name: "Taxes", cashflow: professiondefault.taxes, isPermanent: true, profession: self)
        
        // crashes on the next line
        let otherExpenses = Expense(name: "Other Expenses", cashflow: professiondefault.otherExpenses, isPermanent: true, profession: self)
        
    }
    
}


@Model
class Expense {
    let id: UUID
    var cashflow: Int
    let isPermanent: Bool
    var name: String
    
    var profession: Profession? 
    
    init(name: String, cashflow: Int, isPermanent: Bool, profession: Profession? = nil) {
        self.id = UUID()
        self.cashflow = cashflow
        self.isPermanent = isPermanent
        self.name = name
        self.profession = profession
    }
    
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img