I was following Pauls SwiftData tutorial where he inserts a User into modelContext then pass it as a Bindable to the editView. I am attmepting to do the same but with an Inverse relationship with an array of another object:
I have an inverse relationship between workout and Exercise like so:
@Model```
class Workout: Identifiable {
var id: UUID
var name: String
var exercises: [Exercise]?
var startTime: Date
var endTime: Date
@Model
class Exercise: Identifiable {
var id: UUID
var exerciseName: ExerciseName
var sets: [ExerciseSet]?
var date: Date
@Relationship(inverse: \Workout.exercises) var workout: Workout
>
> Then as was insutrcted in the tutorial i inserted a workout and appended it to NavigationPath():
Button {
let workout = Workout(id: UUID())
modelContext.insert(workout)
path.append(workout)
}
> in EditWorkoutView I accept the workout as a Bindable then pass that Bindable onto the view that the Sheet presents whcih will insert a new Exercise:
struct EditWorkoutView: View {
@Bindable var workout: Workout //SwiftData will update all this on its own as soon as you bind this to a TextField
@State private var showExercieSheet = false
var body: some View {
VStack {
List {
Section(header: Text("Workout Details")) {
TextField("Enter workout name",text: $workout.name)
DatePicker("Start Time", selection: $workout.startTime)
DatePicker("End Time", selection: $workout.endTime)
}
Section(header: Text("Exercises")) {
if let exercises = workout.exercises {
ForEach(exercises){ exercise in
VStack {
Text("\(exercise.exerciseName)")
}
}
.onDelete(perform: delete)
} else {
Text("No Exericses Yet")
}
}
Section {
Button {
showExercieSheet = true
} label: {
Text("Add Exercise")
}
}
}
}
.sheet(isPresented: $showExercieSheet) {
AddExerciseView(workout: workout)
.presentationDragIndicator(.visible)
}
>
> Then In the sheet i insert an Exercise into modelContext using that @Bindable Workout as its workout:
@Bindable var workout: Workout
Button {
//check If selection isnt empty than create an Exercise with this(selection) as the ExerciseName
//and dismiss this sheet
if(selection != nil) {
let newExercise = Exercise(id: UUID(),exerciseName: selection!, date: Date.now, workout: workout)
modelContext.insert(newExercise)
//Uncommenting this causes the error: Unsupported relationship key path ReferenceWritableKeyPath<Exercise, Workout>
//workout.exercises?.append(newExercise)
dismiss()
}
}
> This all works as it should but i can only see the changes when i regfresh the app, from what i undestood SwiftData is supposed to update the @bindable itself why do i need to restart the app to see the new exercise added to the workout? i
i am sorry about the layout issues with the question its very difficult to use this editor