GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

How do you use @Bindable togheter with inferred relationship?

Forums > SwiftUI

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

   

@ladida! Welcome to the forum. Please HELP us to HELP you!

SwiftUI and Markdown: Follow the Rules


@ladida is flummoxed:

i am sorry about the layout issues with the question its very difficult to use this editor

If it's difficult to read your code, you'll find we'll still help. However, if your code is easy to read, it's simple for us to copy and paste it into our own xcode to try.

Regular contributor @roosterBoy gave great advice about posting code. It's easy! Just follow the markup rules.

See -> How to post code

   

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.