The problem was that the JSON data wasn't loaded before I ran the code to check the current date. I moved that code from the onAppear in the ContentView to the following.
DispatchQueue.main.async {
self?.user = user
self?.user.checkDate()
}
The checkDate() function is what called that newMonth() function.
That's why it was printing two different months. It would print the default value for the User struct, and then once the data was loaded, it would print the month that was stored.
Post
Replies
Boosts
Views
Activity
Well, I resolved it not long after posting. I had to make PostContainer a struct and update the containers themselves using the index. See example below.
if var index = self.containers.firstIndex(where: {$0.subreddit == subreddit}) {
// Update previous container
self.containers[index].timestamp = Date()
self.containers[index].posts = posts
self.containers[index].active = true
print("Updated old container")
} else {
// Create the container
// Set to active, because its a new query
let container = PostContainer(timestamp: Date(), subreddit: subreddit, posts: posts, type: .search, active: true)
self.containers.append(container)
print("Created new search container")
}
I guess because with structs, you're creating a new version of the object when it's updated and @Published is able to see the change. With class, it seems to struggle to know if there's been a change or not.