0

in my current project I work with an EnvironmentObject but I have a very strange bug, maybe someone can help.

First the code:

Model:

class Daten: ObservableObject{

@Published var intervallInsgesamt: Double = UserDefaults.standard.double(forKey: Keys.intervallInsgesamt){
    didSet{
        UserDefaults.standard.set(self.intervallInsgesamt, forKey: Keys.intervallInsgesamt)
    }
}

@Published var pauseInsgesamt: Double = UserDefaults.standard.double(forKey: Keys.pauseInsgesamt ){
    didSet{
        UserDefaults.standard.set(self.pauseInsgesamt, forKey: Keys.pauseInsgesamt)
    }
}
... 
}

First View:

@EnvironmentObject var daten: Daten

var body: some View {
    NavigationView{
        GeometryReader { geometry in
            VStack{
                ScrollView(showsIndicators: false){
                    ...
                }

               //IMPORTANT START
                NavigationLink(destination: TrainingView().environmentObject(daten)){ //!!!
                    Text("Start")
                        .styleButton()
                 }
               //IMPORTANT END
            }
        }
    }

}

Second View (TraingsView):

struct TrainingView: View {

@EnvironmentObject var daten: Daten

@State private var isRunning = false
@State private var to: CGFloat = 0

@State private var insgesamt = 0.0
@State private var minuten = 0
@State private var sekunden = 0
@State private var durchgang = 1

@State private var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

@State private var zähl = 0

@State private var color = Color.red

@State private var übung = 0

@State private var momentaneÜbung = ""
@State private var nächsteÜbung = ""

@State private var teilerTo = 0

var body: some View {
    ZStack{
        VStack{
            ...
            }
            HStack(spacing: 50){
                
                Button(action:{

                    isRunning = true
                    Sounds.playSounds(soundfile: "sound.wav")
                    
                }) {
                    Text("Start")
                }
                
                Button(action:{
                    isRunning = false
                    Sounds.playSounds(soundfile: "sound.wav")
                }) {
                    Text("Stop")
                }
            }
        }
    }
    .onReceive(timer) { _ in
        if isRunning{
            if insgesamt > 0{
                print("1. Durchgang: \(durchgang), Übung: \(übung), momenaten: \(momentaneÜbung), nächste: \(nächsteÜbung)")
                insgesamt -= 1.0
                minuten = Int(insgesamt/60)
                sekunden = Int(insgesamt.truncatingRemainder(dividingBy: 60))
                zähl += 1
                
                withAnimation(.default) {
                    to = CGFloat(Double(zähl) / Double(teilerTo))
                    
                }
            }else{
                ... stuff that should run when insgesamt <= 0 but is not important for my problem 
            }
        }
    }
    .onAppear {
        teilerTo = Int(daten.intervallInsgesamt)
        print("Teiler: \(teilerTo)")
        insgesamt = daten.intervallInsgesamt
        
        minuten = Int(insgesamt/60)
        sekunden = Int(insgesamt.truncatingRemainder(dividingBy: 60))
        
        if !daten.übungen.isEmpty{
            momentaneÜbung = daten.übungen[übung].name
            if übung+1 < daten.übungen.count{
                nächsteÜbung = "Pause"
            }else{
                nächsteÜbung = "Nächster Durchgang"
            }
        }
    }
}
}

NOW THE PROBLEM: When I start the app, set the different times, go to the TrainingsView, everything works fine. Then I go back to the first View(change the times or not, it doesn't matter) and go back to the TraingsView. Now nothing works! It doesn't find the EnvironmentObject anymore. I don't know why because the first time it did. Does somebody know why?

(An other problem I is, that print() doesn't work. I can't see the things that should print out. I don't know if that is important...)

Thanks in advance!

Lukas
  • 251
  • 2
  • 13

1 Answers1

0

In your root view, you should declare your object as an observable object:

@ObservableObject var daten = Daten()

then you should use a .environmentObject() modifier on top of your first View:

// pass the daten object that was declared as observable object here
.environmentObject(daten)

from now on, on other descendant views, you can get your model by declaring it as an environment object:

@EnvironmentObject var daten: Daten 
// gives you the same old daten model
Mahdi BM
  • 1,826
  • 13
  • 16