Hello,
I have an app that uses JSON data to save and load information. This is how the information is loaded when the app is first opened.
DispatchQueue.global(qos: .background).async { [weak self] in
//Get data from "user.data"
guard let data = try? Data(contentsOf: Self.fileURL) else {
//code to use test data
/*#if DEBUG
DispatchQueue.main.async {
self?.scrums = DailyScrum.data
}
#endif*/
return
}
guard let user = try? JSONDecoder().decode(User.self, from: data) else {
fatalError("Can't decode user data")
}
//Set published value to user that was loaded from the file
DispatchQueue.main.async {
self?.user = user
}
}
}
There is a struct called Month that is used inside of another struct called User. When the app is open, in the onAppear in ContentView, it runs a function to check the current date. It compares it against what's already saved, and if it's a new month, it will create a new Month struct to replace the current month.
private mutating func newMonth() {
//Create new month
month = Month(name: Date().month)
}
This seems to be working, but not exactly as intended. The new month will be created, but when I open the app it will still show the previous month. I have a TextView that displays the month. I printed the month variable to the console to see what it was reading.
Printed in an HStack this way
let _ = print(user.month)
Output
Month(name: "July", transactions: [], income: 0.0, expenses: 0.0, net: 0.0, saved: 0.0, invested: 0.0, netMargin: 0.0)
Month(name: "June", transactions: [], income: 0.0, expenses: 0.0, net: 0.0, saved: 0.0, invested: 0.0, netMargin: 0.0)
Somehow that month variable is showing two different months in it, June and July. It doesn't make sense to me, because it's not an array and that month variable should be getting replaced due to the newMonth() function. What's going on here? How is it creating a new month, but not overwriting the previous month? Let me know if you need anymore information.