SwiftData Relationship

I have 2 classes and a relationship from one to another... I am getting an error while runtime. Can someone help me with this issue??..

    @storageRestrictions(accesses: _$backingData, initializes: _trans)
        init(initialValue) {
                    _$backingData.setValue(forKey: \.trans, to: initialValue)
                    _trans = _SwiftDataNoType()
        }

    get {
                    _$observationRegistrar.access(self, keyPath: \.trans)
                    return self.getValue(forKey: \.trans)
        }

    set {
                    _$observationRegistrar.withMutation(of: self, keyPath: \.trans) {
                            self.setValue(forKey: \.trans, to: newValue)
                    }
        }
}

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x25256ead8) Below is my class...

import SwiftData

@Model
final class Main {
    var name: String
    var limit: Double
    
    @Relationship(deleteRule: .cascade)
    var trans: [Child] = []
    
    init(name: String, limit: Double, trans: [Child]) {
        self.name = name
        self.limit = limit
        self.trans = trans
    }
}

@Model
final class Child {
    var trip: String
    var distance: Double

    init(trip: String, distance: Double) {
        self.trip = trip
        self.distance = distance
    }
}
  1. Your relationship should have a reference to the model(s) on both sides. Looks like you're missing a property on the Child model to refer to the Main model.
  2. I think you need the inverse attribute on your relationship on Main to identify the property
  3. I've only been able to get it to work if both sides of the relationship are optionals, even if the relationship is mandatory. So you need an optional trans array rather than an empty array.
  4. You'd create or fetch an instance of the Main model first, before creating the Child instances. Then create the Child instances, passing in a reference to their parent.
  5. You don't have to use an optional in the Child initializer to avoid accidentally leaving it nil.
@Model
final class Main {
    var name: String
    var limit: Double
    
    @Relationship(deleteRule: .cascade, inverse: \Child.parent)
    var trans: [Child]?
    
    init(name: String, limit: Double) {
        self.name = name
        self.limit = limit
    }
}

@Model
final class Child {
    var trip: String
    var distance: Double
    var parent: Main?

    init(trip: String, distance: Double, parent: Main) {
        self.trip = trip
        self.distance = distance
        self.parent = parent
    }
}

let parent = Main(name: "", limit: 0)
let child1 = Child(trip: "", distance: 0, parent: parent)
let child2 = Child(trip: "", distance: 0, parent: parent)

modelContext.insert(parent)
modelContext.insert(child1)
modelContext.insert(child2)
modelContext.save()

// the next time you fetch the parent, the children will be present in the `trans` array
try modelContext.fetch(FetchDescriptor<Main>())

// or if you need them immediately in the code you're executing, you can assign them:
parent.trans = [child1, child2]

Got the same Issue at one out of 5 relations. Did the last comment solve the issue for you?

SwiftData Relationship
 
 
Q