|
I'm using a class which is ObservableObject to publish data used by several views in my App. There's a strange issue where I call a function to change a value which doesn't get updated in the view. I've tested using another @State variable just called "update" to force an update and that works. OK, not explaining it well but here's what I have: Class A - ObseravbleObject Published is an Array of Class B not an ObservableObject At the top of my App I create ObjectA which is of Class A and populate the array with Class B objects and use .environmentObject( ObjectA ) View A has @EnvironmentObject ObjectA. View B and View C are children of View A. View B displays a variable from the Array of Class B. View C has a button which when pressed changes the variable via a method of Class B. When I hit the button nothing changes, if I get the view to redraw then View B shows the new value. I've tried making Class B an ObservableObject as well and Publishing the variable but it still doesn't work. I guess having Class B inside an array means Class A can't see the change and tell the views to redraw. Any idea what's the right way to do this? |
|
hi, visual update issues are more common than you think, and there's probably no one right way to get updates done right in the situation you describe. here's one possible solution:
here's a relevant example: the main view shows the list of items held by a Class A object and navigates to sort of a detail view to show and edit information for one of the Class B objects
(the code is the closest i could come to matching up with your description. if you have other code that doesn't quite fit my example, please consider posting it.) hope that helps, DMG |
|
Thank you so much for taking the time to explain all that, I'll give it a try and let you know how I get on! |
|
I really appreciate your solution but I wanted to see if there was something simpler, I'm sure my description overcomplicated what was going on in my code. In the end both my child views are passed an object from the Array which is of Class B. Something like this:
I made sure Class B was also ObservableObject and @Published the variable that's getting set. I changed the code in View B to this:
Doing that fixed the problem I was having immediately. I'm sure I'll have to learn about Combine later on but I'm happy with this solution. @EnvironmentObject makes it easy for me to access my data from any of my views and when an item I need that's stored by reference I can use @ObservedObject to get updates from it. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.