I am creating an app where you can get coins and xp points for level ups. I have the problem that if I use global variables they simply don’t change the number on the Views (Home and Shop) and when I used a class for that (with @environmentObject and @published) they only change in one view. eg: if I add xp points in home it changes the level eg. from 1 to 2 and then resets the xp to 0, but it doesn’t change it in the shop, it still shows level 1 and 6/10 xp. But it should be level 2 and 0/10 xp. Can anyone help?
Asked
Active
Viewed 173 times
1 Answers
0
Your approach with @environmentObject and @Published is right. You need a central state holder, ViewModel, etc. If you create an instance of it at the beginning and pass it as an environmentObject where you need it then this single instance should be accessible at every view. In the views you can subscribe to the @Published variables.

Kerasinos Apps
- 31
- 2