Fatal error: Unsupported relationship key path ReferenceWritableKeyPath when trying to append to a variables array

I have a SwiftData model where a Workout has an array of Exercises and the Exercise Model has an array of Sets:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@Model
class Workout: Identifiable {
var id: UUID
var name: String?
@Relationship(deleteRule: .cascade) //when deleting a workout you delete all exericses that happened during that workout
var exercises: [Exercise]?
@Model
class Exercise: Identifiable {
var id: UUID
var exerciseName: ExerciseName
@Relationship(deleteRule: .cascade,inverse: Set.exercise) var sets: [Set]? //When an Exercise gets Deleted all Sets associated need to get deleted
var date: Date
@Model
class Set: Identifiable {
var id: UUID
var weight: Int
var reps: Int
var isCompleted: Bool
var exercise: Exercise
</code>
<code>@Model class Workout: Identifiable { var id: UUID var name: String? @Relationship(deleteRule: .cascade) //when deleting a workout you delete all exericses that happened during that workout var exercises: [Exercise]? @Model class Exercise: Identifiable { var id: UUID var exerciseName: ExerciseName @Relationship(deleteRule: .cascade,inverse: Set.exercise) var sets: [Set]? //When an Exercise gets Deleted all Sets associated need to get deleted var date: Date @Model class Set: Identifiable { var id: UUID var weight: Int var reps: Int var isCompleted: Bool var exercise: Exercise </code>
@Model
class Workout: Identifiable {
    var id: UUID
    var name: String?    
    @Relationship(deleteRule: .cascade) //when deleting a workout you delete all exericses that happened during that workout
    var exercises: [Exercise]?

@Model
class Exercise: Identifiable {
    var id: UUID
    var exerciseName: ExerciseName
    
    @Relationship(deleteRule: .cascade,inverse: Set.exercise) var sets: [Set]?    //When an Exercise gets Deleted all Sets associated need to get deleted
    var date: Date


@Model
class Set: Identifiable {
    var id: UUID
    var weight: Int
    var reps: Int
    var isCompleted: Bool
    var exercise: Exercise

The way my add works when the user Navigates to addExercise view I create a local temporary Workout object that I will add to the modelContext when the user is done editing it. The user can then create an exercise here and it will appear in a list of exercise that will take the user to an Edit Exercise View:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> Section(header: Text("Exercises")) {
if let exercises = workout.exercises {
ForEach(exercises){ exercise in
NavigationLink(value: exercise) {
Text(exercise.exerciseName.name)
.padding(.top, 5)
.padding(.bottom, 5)
}
}
.onDelete(perform: delete)
} else {
Text("No Exericses Yet")
}
}
.navigationDestination(for: Exercise.self) { exercise in
EditExeriseView(exercise: exercise, sets: exercise.sets ?? [])
.navigationBarBackButtonHidden(true)
}
</code>
<code> Section(header: Text("Exercises")) { if let exercises = workout.exercises { ForEach(exercises){ exercise in NavigationLink(value: exercise) { Text(exercise.exerciseName.name) .padding(.top, 5) .padding(.bottom, 5) } } .onDelete(perform: delete) } else { Text("No Exericses Yet") } } .navigationDestination(for: Exercise.self) { exercise in EditExeriseView(exercise: exercise, sets: exercise.sets ?? []) .navigationBarBackButtonHidden(true) } </code>
            Section(header: Text("Exercises")) {
                if let exercises =  workout.exercises {
                    ForEach(exercises){ exercise in
                        NavigationLink(value: exercise) {
                            Text(exercise.exerciseName.name)
                                .padding(.top, 5)
                                .padding(.bottom, 5)
                        }
                    }
                    .onDelete(perform: delete)
                } else {
                    Text("No Exericses Yet")
                }
            }

        .navigationDestination(for: Exercise.self) { exercise in
            EditExeriseView(exercise: exercise, sets: exercise.sets ?? [])
                .navigationBarBackButtonHidden(true)
        }

In the Edit Exercise view I check if the passed Exercise has Sets and generate local temporary ones If it doesn’t, the user can then add Sets to this temporary set array. When the user clicks save the follow function is called:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>func save() {
//first inserting the sets into the Sets model
for set in sets {
if(set.isCompleted){
modelContext.insert(set)
//xcode thinks i am trying to write to this actual exerises model context a set but im just rtying to add it to a local exercise object back in the AddworkoutView
exercise.sets?.append(set)
print("(exercise.sets!)")
print("(sets)")
} else {
print("this set was not toggled")
modelContext.delete(set)
}
}
//finished iterating through the sets and appending them so we can go back
dismiss()
}
</code>
<code>func save() { //first inserting the sets into the Sets model for set in sets { if(set.isCompleted){ modelContext.insert(set) //xcode thinks i am trying to write to this actual exerises model context a set but im just rtying to add it to a local exercise object back in the AddworkoutView exercise.sets?.append(set) print("(exercise.sets!)") print("(sets)") } else { print("this set was not toggled") modelContext.delete(set) } } //finished iterating through the sets and appending them so we can go back dismiss() } </code>
func save() {
    //first inserting the sets into the Sets model
    for set in sets {
        if(set.isCompleted){
            modelContext.insert(set)
            //xcode thinks i am trying to write to this actual exerises model context a set but im just rtying to add it to a local exercise object back in the AddworkoutView
            exercise.sets?.append(set)
            print("(exercise.sets!)")
            print("(sets)")
        } else {

            print("this set was not toggled")
            modelContext.delete(set)
        }
        
    }
    //finished iterating through the sets and appending them so we can go back
    dismiss()
}

I have confirmed this adds the set to the exercises sets array but the problem is I can only see the changes when I rerun the entire app. I have tried to manually add the sets to the passed exercise but Xcode thinks im trying to write it to SwiftData which is impossible because of the inverse relationship. Is there any way to see the changes without refreshing the app when the user navigates back to AddWorkoutView?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật