I’m using UserDefaults with SwiftUI to save my data from multiple variables and I use those values for graphs. My goal would be to only show the user daily progress, so I would need to delete the stored data from UserDefaults or reset the variable values back to 0 in every day. I assume Apple Fitness app does something similar with the ‘Activity rings’ which resets every day, but I don’t know how to achieve something like this. Every help would be appreciated.
My code:
@Published var waterGraph: Float = UserDefaults.standard.float(forKey: "waterGraph") {
didSet {
UserDefaults.standard.set(self.waterGraph, forKey: "waterGraph")
}
}
There are any different ways of doing this, it al depends on how your data "lives" and we can't be specific without more information.
One option could be to make your
waterGraph
object depend on a dateWaterGraph(date: Date)
I would assume that Apple does something like this.Another option is to check and see if you need new data when the view appears.