I have a class that manages my NSPersistentContainer for my CloudKit/CoreData. In a view to access its contents or update I use an @EnvironmentObject and call the methods and it works perfectly.
The problem is now I need to use it in a class where I can't access the environment object. So I added a shared property to the my class to access it. It works fine but the published variables don't seem to update on my views when triggered by the class.
A simple example looks as followed:
My Datacontroller:
class DataController: ObservableObject {
static let shared = DataController()
//Coredatastack would be here
let container: NSPersistentContainer
@Published var remaining: Int = 0
@Published var today: DayProgress? = nil
func getRemaining() {
remaining = Int((today?.dailyGoal ?? 0) - (today?.currDailyTotal ?? 0))
}
If I call getRemaining() anytime after updating the values of the published variable today in a view, then anywhere the environmentObject remaining is accessed will update.
But in a class if I use Datacontroller.shared.getRemaining(), it will not update the published variable remaining on the view. I assume this is because im working with different instances of the Datacontroller() but im not sure how to solve this. Thanks for any hints or tips.
Note: The class that is triggers the updates the is triggered from outside the app (shortcut) and I have a function that calls getRemainder() anytime the app opens