Lose my variables when my watch turn off

Hello guys,

I have such a big coding problem since a half year now about data saving. I have a fitness app where we can add our exercices depend of the muscle, so you can choose it and then select your weight and number of repetitions and valid your exercise. But when I do my exercises and my watch screen is also turn off, my muscle and muscleExercise variables are going back to their default value.

Here some code for you :

@EnvironmentObject var dataManager: DataManager
@Environment(\.modelContext) private var context
    
@AppStorage("savedGroupName") private var savedGroupName: String = ""
@AppStorage("savedExerciceName") private var savedExerciceName: String = ""
    
    
@State var groupName: String = "À choisir"
@State var ExerciceChoose: String = "À choisir"

I use my variables here :

HStack {
                            Text("Muscle:")
                            Spacer()
                            NavigationLink(destination: MusclesView()) {
                                Text(savedGroupName.isEmpty ? "À choisir" : savedGroupName)
                            }
                            
                            .buttonStyle(PlainButtonStyle())
                        }
                        .onAppear {
                            savedGroupName = groupName
                        }




HStack {
                            Text("Exercise:")
                            Spacer()
                            NavigationLink(destination: MuscleExercicesView(groupName: groupName, ExerciceChoose: ExerciceChoose)) {
                                Text(savedExerciceName.isEmpty ? "À choisir" : savedExerciceName)
                            }
                            .onAppear {
                                savedExerciceName = ExerciceChoose
                                
                            }
                            .buttonStyle(PlainButtonStyle())
                        }

The value of my muscle variable is taking in an other view :



struct MusclesView: View {
        
    let muscleGroup = ExerciceData.muscleGroups
    
    var body: some View {
        NavigationStack {
            List(muscleGroup, id: \.name) { group in
                NavigationLink(destination: MuscleExercicesView(groupName: group.name, ExerciceChoose: "À choisir")) {
                    Text(group.name)
                }
            }
           
        }
    }
}

#Preview {
    MusclesView()
}

and the value of the exerciseMuscle variable is also taking in an other view :
```import SwiftUI


struct MuscleExercicesView: View {
    
    let exerciceGroup = ExerciceData.muscleGroups
    
    @State var groupName: String
    @State var ExerciceChoose: String

    
    
    var body: some View {
        if let group = ExerciceData.muscleGroups.first(where: { $0.name == groupName }) {
            List(group.exercices, id: \.id) { exercice in
                NavigationLink(exercice.name, destination: CurrentInformationsView(groupName: groupName, ExerciceChoose: exercice.name))
                   
            }
            /*.onTapGesture {
                print("Selected exercise: \(ExerciceChoose) for muscle group: \(groupName)")
            }*/
            .navigationTitle(Text("Exercices pour \(groupName)"))
        } else {
            Text("Aucun exercice trouvé pour \(groupName)")
                .navigationTitle(Text("Erreur"))
        }
        
    }
}

#Preview {
    MuscleExercicesView(groupName: "Pectoraux", ExerciceChoose: "À choisir")
}

I tried many things (like userDefault, put my values in an array to save it etc..) to keep my variables with the same value during the session but nothing works. I wish I could have some help from you guys.

Have a good day !

Cyrille

Lose my variables when my watch turn off
 
 
Q