I want to update the Published property of my ChallengeManager class with data passed in from LocationManager. Here is the simplified code with the relevant bits:
LocationManager
final class LocationManager: NSObject, ObservableObject {
var challengeManager = ChallengeManager()
...
//a func called from locationManager delegate converts the region to an instance of an Area object then calls a method on the ChallengeManager class like this:
challengeManager.loadChallenge(for: activeArea)
...
ChallengeManager
final class ChallengeManager: ObservableObject {
@Published var isShowingChallenge = false
@Published var challengeToDisplay: Challenge?
func loadChallenge(for area: Area) {
if let challenge = area.challenge { //gets challenge property of area object
self.challengeToDisplay = challenge
self.isShowingChallenge = true
}
}
Finally, the ContentView:
struct ContentView: View {
@ObservedObject var challengeManager = ChallengeManager()
...
(To be honest, I can get the results I want by adding an ObservedObject for the LocationManager in the View and then passing the values into a func there. But I don't like the idea of doing this for multiple views. And I also want loadChallenge() to do more heavy lifting. It seems to me that it should be the single source of truth. No?)
The problem:
If I try to access challengeManager.challengeToDisplay inside the ContentView, the value is always nil.
Print statements in the loadChallenge() func tell me that the value from the locationManager is being received correctly. But @Published var challengeToDisplay is not changing.
Can someone please tell me what I am doing wrong?
Thanks!